回到 FragmentActivity 后出现 IllegalStateException
Posted
技术标签:
【中文标题】回到 FragmentActivity 后出现 IllegalStateException【英文标题】:IllegalStateException after coming back to a FragmentActivity 【发布时间】:2012-01-26 07:23:32 【问题描述】:这是关于我发现我的应用程序面临的一个经常性问题,并且与使用几个 DialogFragment 相关。我主要针对平台级别 8 设备,因此要使用 DialogFragments,我必须使用兼容性库。
每当我退出应用程序并且他们回来时,问题本身就会发生。这是堆栈跟踪:
12-22 14:56:05.819: W/dalvikvm(6384): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
12-22 14:56:05.949: E/androidRuntime(6384): FATAL EXCEPTION: main
12-22 14:56:05.949: E/AndroidRuntime(6384): java.lang.IllegalStateException: Activity has been destroyed
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1269)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:529)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:102)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.view.ViewPager.populate(ViewPager.java:692)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:851)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.View.measure(View.java:8171)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.os.Looper.loop(Looper.java:123)
12-22 14:56:05.949: E/AndroidRuntime(6384): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 14:56:05.949: E/AndroidRuntime(6384): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 14:56:05.949: E/AndroidRuntime(6384): at java.lang.reflect.Method.invoke(Method.java:521)
12-22 14:56:05.949: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-22 14:56:05.949: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-22 14:56:05.949: E/AndroidRuntime(6384): at dalvik.system.NativeStart.main(Native Method)
这是我精简到最小的 MainActivity。
public class MainActivity extends FragmentActivity
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
public void showNewChannelFragmentDialog()
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null)
ft.remove(prev);
ft.addToBackStack(null);
// Create and show the dialog.
NewChannelDialogFragment newFragment = NewChannelDialogFragment.newInstance();
newFragment.setContext(this);
newFragment.show(ft,"dialog");
showNewChannelFragmentDialog() 方法只在用户选择特定菜单选项时调用,所以我认为 DialogFragment 本身的内容在这里无关紧要。
但是您可以看到我完全按照here 所示的方式进行操作,并且确实有效,正如我之前所说的,问题是每当我的应用程序在用户离开后恢复正常。
有什么建议吗?我正在考虑删除片段并仅使用常规对话框。
【问题讨论】:
你能显示你在哪里调用 showNewChannelFragmentDialog 吗?我想如果你评论这个方法的内容,当你重新启动你的应用程序时你没有收到错误,不是吗? 【参考方案1】:也许这不是唯一的问题,但你应该打电话
ft.add(newFragment);
ft.commit(); //but at least this
在 showNewChannelFragmentDialog() 函数的末尾。
【讨论】:
以上是关于回到 FragmentActivity 后出现 IllegalStateException的主要内容,如果未能解决你的问题,请参考以下文章
将参数从 Android FragmentActivity 传递给 Fragment
(未解决)Android FragmentActivity 错误