AsycTask Werfen IllegalStateException - Fragment Nicht An Aktivität

Habe ich Folgendes AsyncTask in meiner Android-Anwendung. Dieser AsyncTask ist mit enthalten in der OnCreate () - Methode einer Klasse, die Sie erweitert PreferenceFragment.

public class NotificationsPreferenceFragment extends PreferenceFragment {

private static Context context;

public NotificationsPreferenceFragment() {

}

public NotificationsPreferenceFragment(Context context) {
    this.context = context;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref_notifications);

    getPreferenceManager().findPreference(getString(R.string.send_all_notifications))
            .setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {

                        class NotificationSendTask extends DialogAsyncTask {

                            public static final String TAG = "NotificationFragment";

                            public NotificationSendTask(Activity activity, String dialogMsg) {
                                super(activity, dialogMsg);
                            }

                            @Override
                            protected String doInBackground(String... params) {
                                String url = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(getString(R.string.notification_web_service_url), getString(R.string.default_notification_web_service_url));

                                if (NetworkingHelper.isNetworkAvailable(getActivity())) {
                                    NotificationDao notificationDao = new NotificationDaoImpl(DatabaseManager.getInstance(getActivity().getApplicationContext()), getActivity().getApplicationContext());

                                    List<Notification> unsentNotificationList = notificationDao.findAllNotSent();
                                    if (unsentNotificationList.size() != 0) {
                                        NotificationSenderTask ns = new NotificationSenderTask(url, context);
                                        try {
                                            if (ns.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (unsentNotificationList)).get()) {
                                                return getString(R.string.success);
                                            }
                                        } catch (InterruptedException e) {
                                            Log.e(TAG, e.getMessage());
                                        } catch (ExecutionException e) {
                                            Log.e(TAG, e.getMessage());
                                        }

                                        return getString(R.string.failed_to_send_notifications);
                                    } else {
                                        return getString(R.string.no_notifications_to_send);
                                    }
                                } else {
                                    return getString(R.string.no_connection_notifications);
                                }
                            }

                            public void onPostExecute(String result) {
                                super.onPostExecute(result);
                                if (dialog != null && dialog.isShowing()) {
                                    dialog.hide();
                                }
                                Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
                            }
                        }
                        NotificationSendTask notificationSendTask = new NotificationSendTask(getActivity(), "Sending unsent notifications...");
                        notificationSendTask.execute();
                        return true;

                }
            });

    getPreferenceManager().findPreference(getString(R.string.export_notifications)).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            NotificationExportTask notificationExportTask = new NotificationExportTask(NotificationsPreferenceFragment.this.getActivity(), 1);
            notificationExportTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return true;
        }
    });
}
}

Ich bin immer folgende exception:

java.lang.IllegalStateException: Fragment NotificationsPreferenceFragment{416092f8} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:741)
at android.app.Fragment.getString(Fragment.java:763)

Kann mir jemand bitte erklären, warum dies passiert ist und schlage vor, Möglichkeiten, um dieses Problem zu beheben?

UPDATE:

Hier ist der code für die Aktivität:

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.pref_headers, target);
}
}
Diese Klasse ist nicht auf den code, den Sie zeigte. Bitte zeigen Sie uns den rest des Codes.
Der code wurde Hinzugefügt.

InformationsquelleAutor COBOL | 2014-08-12

Schreibe einen Kommentar