这是关于一个反复出现的问题,我发现我的应用程序正面临着一个问题,并且与使用几个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所示进行操作,并且确实可以工作,正如我之前说的,问题在于,一旦用户离开应用程序,我的应用程序就会恢复正常。

有什么建议么?我正在考虑删除片段,而仅使用常规对话框。

最佳答案

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

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


在showNewChannelFragmentDialog()函数的末尾。

09-11 21:32