Android - 第二次启动应用程序崩溃(FragmentManager问题)

时间:2013-02-19 15:45:22

标签: android android-fragments

我第一次启动应用程序,一切正常。我使用finish().

完成了我的活动

在此之后,我再次启动我的应用程序并且崩溃了这个日志:

02-19 15:33:16.652: E/AndroidRuntime(1859): FATAL EXCEPTION: main
02-19 15:33:16.652: E/AndroidRuntime(1859): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fileexplorermanager/com.landa.fileexplorermanager.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.os.Looper.loop(Looper.java:137)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at java.lang.reflect.Method.invoke(Method.java:511)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at dalvik.system.NativeStart.main(Native Method)
02-19 15:33:16.652: E/AndroidRuntime(1859): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at com.landa.features.BrowseHandler.populateContent(BrowseHandler.java:137)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at com.landa.features.BrowseHandler.openFile(BrowseHandler.java:98)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at com.landa.fileexplorermanager.MainActivity.onCreate(MainActivity.java:33)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.Activity.performCreate(Activity.java:5104)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-19 15:33:16.652: E/AndroidRuntime(1859):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-19 15:33:16.652: E/AndroidRuntime(1859):     ... 11 more

看来问题是populateContent()函数调用了FragmentManager的提交 - 这就是它失败的行:

public void populateContent(File f)
{
    FragmentTransaction transaction = ac.getSupportFragmentManager().beginTransaction();
    ContentFragment cf = new ContentFragment(ctx, ac, this, f);

    transaction.replace(R.id.contentFragment, cf);
    transaction.addToBackStack(null);

    updateShownPath(f.getPath());
    transaction.commit();
}

现在,我不知道为什么第一次应用程序运行正常,然后是finish(),然后在第二次启动时崩溃。

任何提示?

注意:在完成应用程序(总是)之前,我在FragmentManager中有一个片段 - 这是主片段。

这是onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initialiseApplication();

    browseHandler.openFile(new File(BrowseHandler.current_path));

}

1 个答案:

答案 0 :(得分:1)

这个错误的原因似乎是你在已经被破坏的上下文中调用commit,即活动已经完成。

确保您的ctx变量引用了有效Activity。或者,您可能希望将Activity保留在后台,而不是手动完成。