返回FragmentActivity后出现IllegalStateException

时间:2011-12-22 17:15:37

标签: android android-fragments illegalstateexception android-dialogfragment

这是关于我发现我的应用程序面临的一个经常出现的问题,并且与使用几个DialogFragments有关。我主要针对平台级别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显示的内容确实有效,正如我之前所说的那样,每当我的应用程序恢复生命后,一旦用户离开它,就会出现问题。

有什么建议吗?我正在考虑丢弃碎片,只使用常规的Dialogs。

1 个答案:

答案 0 :(得分:0)

也许这不是唯一的问题,但你应该致电

ft.add(newFragment);
ft.commit(); //but at least this

showNewChannelFragmentDialog()函数的末尾。