[英]Fragment Crash on Activity destroyed
I am facing this particular crash on few devices 我在几个设备上都遇到这种特殊的崩溃
Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:617)
at net.trellisys.papertrell.components.mediagallery.MGBaseActivity$initQuickControls.doInBackground(MGBaseActivity.java:136)
at net.trellisys.papertrell.components.mediagallery.MGBaseActivity$initQuickControls.doInBackground(MGBaseActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
What I am doing is am loading a Fragment
to an Activity
using an Async
method. 我正在做的是使用
Async
方法将Fragment
加载到Activity
。
I can not recreate this error in my side but have seen this issue in many devices. 我无法在自己的身边重新创建此错误,但是在许多设备中都已看到此问题。 Can any one help me out with this error?
有人可以帮助我解决此错误吗?
Here's the code snippet :- 这是代码片段:-
public class initQuickControls extends AsyncTask<String,Void,String> {
String fromPage = "";
public initQuickControls(String from){
fromPage = from;
}
QuickControlsFragment quicFrag = null;
@Override
protected String doInBackground(String... strings) {
quicFrag = new QuickControlsFragment(fromPage);
FragmentManager fragManager = getSupportFragmentManager();
fragManager.beginTransaction().replace(R.id.quickcontrols_container,quicFrag).commitAllowingStateLoss();
return "EXECUTED";
}
protected void onPostExecute(String result) {}
}
And this is how I am calling the Fragment
:- 这就是我所说的
Fragment
:-
new initQuickControls("List").execute("");
First of all you should be familiar with the activity lifecycle . 首先,您应该熟悉活动生命周期 。
I think that your AsynkTask finishes when activity is destroyed and that's why you cannot perform any fragment transaction. 我认为活动被破坏时,您的AsynkTask完成了,这就是为什么您无法执行任何片段事务。 You may add condition before this transaction:
您可以在此交易之前添加条件:
if (!MainActivity.this.isFinishing()) {...}
Hope it'll help. 希望能对您有所帮助。
But if you're using AsynkTask
only to add fragment - this is incorrect. 但是,如果您仅使用
AsynkTask
添加片段-这是不正确的。 Just add your fragment in the onCreate
method without any additional code. 只需将片段添加到
onCreate
方法中,而无需任何其他代码。
edit: 编辑:
You cannot perform UI operations in the doInBackground
method because it's running not in the UI thread. 您无法在
doInBackground
方法中执行UI操作,因为该方法未在UI线程中运行。 Move Ui-specific code to onPostExecute
method and it'll work. 将特定于Ui的代码移动到
onPostExecute
方法,它将起作用。 And do not forget about isFinishing()
condition. 并且不要忘记
isFinishing()
条件。 Also you should cancel all your AsyncTasks
in Activity.onBackPressed()
method to avoid such problems. 另外,您应该取消
Activity.onBackPressed()
方法中的所有AsyncTasks
以避免此类问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.