我试图从方法onPostExecute(asynctask)调用片段......
这是我的代码......
@Override
protected void onPostExecute(String result) {
if (processVisibility != false) {
pd.dismiss();
Toast.makeText(mContext, sendingMessageSuccess,
Toast.LENGTH_SHORT).show();
FragmentExample4 fragment = new FragmentExample4();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
super.onPostExecute(result);
}
当输入方法onPostExecute我的应用程序崩溃时,我收到此错误...
03-01 17:13:42.141 29230-29230/copyworld.assistenzamobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
at copyworld.assistenzamobile.backgroundMail.BackgroundMail$startSendingEmail.onPostExecute(BackgroundMail.java:145)
at copyworld.assistenzamobile.backgroundMail.BackgroundMail$startSendingEmail.onPostExecute(BackgroundMail.java:95)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
我也试试这个......
@Override
protected void onPostExecute(String result) {
if (processVisibility != false) {
pd.dismiss();
Toast.makeText(mContext, sendingMessageSuccess,
Toast.LENGTH_SHORT).show();
FragmentExample4 fragment = new FragmentExample4();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
if (!isFinishing()) {
fragmentTransaction.commit();
}
}
super.onPostExecute(result);
}
但这也行不通,我收到同样的问题..
有人可以帮助我吗?抱歉我的英文
答案 0 :(得分:1)
AsyncTasks在活动被销毁后继续运行。这里发生的事情是你的异步任务在活动被销毁后完成,并尝试访问现在死亡的活动,抛出异常 - 你不能将片段添加到已完成的活动中。如果活动仍然存在,您必须签入onPostExecute。
AsyncTasks很棘手,因为它们包含对活动的引用,并且可能导致此类问题(任务完成时活动已停止)。您可以考虑使用setRetainInstance(true)的片段来运行异步任务,或者可能是IntentService。