android – 为什么每次启动使用支持片段的Activity时都会收到IllegalStateExceptions?

前端之家收集整理的这篇文章主要介绍了android – 为什么每次启动使用支持片段的Activity时都会收到IllegalStateExceptions?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用 Android支持库在precombcomb应用程序中使用片段.

如果我在活动的onCreate方法添加一个片段,则活动会因IllegalStateException而崩溃.

08-04 10:19:49.100: ERROR/AndroidRuntime(18501): FATAL EXCEPTION: main  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501): java.lang.RuntimeException: Unable to start activity ComponentInfo{net..../net.....homescreen...Main}: java.lang.IllegalStateException: Activity has been destroyed  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.os.Handler.dispatchMessage(Handler.java:99)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.os.Looper.loop(Looper.java:123)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.app.ActivityThread.main(ActivityThread.java:3691)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at java.lang.reflect.Method.invokeNative(Native Method)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at java.lang.reflect.Method.invoke(Method.java:507)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at dalvik.system.NativeStart.main(Native Method)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501): Caused by: java.lang.IllegalStateException: Activity has been destroyed  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1257)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:535)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:519)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at net.....AbstractActivity.onCreate(AbstractActivity.java:103)   
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at net.....homescreen...Main.onCreate(..Main.java:51)   
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)  
08-04 10:19:49.100: ERROR/AndroidRuntime(18501):     ... 11 more

AbstractActivity的onCreate看起来像这样:

protected void onCreate(Bundle savedInstanceState) {
    if (menuEnabled) {
        FragmentManager fragmentManager = getCompatibleFragmentManager();
        FragmentTransaction transaction = fragmentManager
                .beginTransaction();
        transaction.add(new OptionsMenuFragment(),OPTIONS_MENU_IDENTIFIER);
        transaction.commit();
    }
    super.onCreate(savedInstanceState);
}

如果我调试调用,我会看到fragmentmanager抛出此异常,因为activity是null.似乎片段管理器根本没有初始化.

我究竟做错了什么?

解决方法

事实证明兼容包中的FragmentActivity在onCreate方法本身中初始化片段管理器.

如果我将调用super移动到onCreate方法的开头,一切正常.

原文链接:https://www.f2er.com/android/313479.html

猜你在找的Android相关文章