0

我第一次启动我的应用程序,一切正常。我使用完成我的活动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

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

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();
}

现在,我不知道为什么应用程序第一次运行良好,然后完成(),然后在第二次启动时崩溃。

有什么提示吗?

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

这是 onCreate():

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

    initialiseApplication();

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

}
4

1 回答 1

1

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

确保您的ctx变量引用一个活动的Activity. 或者,您可能希望将其保留Activity在后台而不是手动完成它。

于 2013-02-19T20:31:45.917 回答