gpt4 book ai didi

java - AsycTask 抛出 IllegalStateException - fragment 未附加到 Activity

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:26 24 4
gpt4 key购买 nike

我的 Android 应用程序中有以下 AsyncTask。此 AsyncTask 包含在扩展 PreferenceFragment 的类的 OnCreate() 方法中。

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;
}
});
}
}

我收到以下异常:

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)

有人可以向我解释为什么会发生这种情况并提出解决此问题的方法吗?

更新:

这是 Activity 的代码:

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);
}
}

最佳答案

因为您正在您的应用程序中执行后台任务。无法保证用户在任务完成之前会停留在同一屏幕上,因此如果用户在任务完成之前导航到其他屏幕或按下主页按钮;您的 fragment 与 Activity 分离。所以一定要确保你有 fragment 附加到 Activity。尝试检查

如果(已添加){
//在这里做你的用户界面
}

在你收到回调的地方添加上面的检查

关于java - AsycTask 抛出 IllegalStateException - fragment 未附加到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25262994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com