片段onDetachFragment()非法状态异常

时间:2019-02-21 10:55:54

标签: android fragment illegalstateexception detach

我以前在我的Fragment中有onDetach,在其中我只是用来释放资源(Context = null和类似的东西)。从那时起,我得到了几个这种非法状态例外。我删除了onDetach,并将代码移到了寿命终止的其他方法。我仍然遇到这个例外,我认为这是来自未更新应用程序的用户,但是在android vitals中,它表示这是来自最新版本的应用程序。为什么会发生这种情况以及如何处理呢?还是仅仅是Google Play控制台提示错误的版本?

java.lang.IllegalStateException:
    at android.support.v4.app.FragmentManagerImpl.detachFragment (FragmentManagerImpl.java)
      or                     .saveAllState (FragmentManagerImpl.java)
      at android.support.v4.app.FragmentController.saveAllState (FragmentController.java)
      at android.support.v4.app.FragmentActivity.onSaveInstanceState (FragmentActivity.java)
      at android.support.v7.app.AppCompatActivity.onSaveInstanceState (AppCompatActivity.java)
      at android.app.Activity.performSaveInstanceState (Activity.java:1337)
      at android.app.Instrumentation.callActivityOnSaveInstanceState (Instrumentation.java:1300)
      at android.app.ActivityThread.callCallActivityOnSaveInstanceState (ActivityThread.java:4131)
      at android.app.ActivityThread.performStopActivityInner (ActivityThread.java:3528)
      at android.app.ActivityThread.handleStopActivity (ActivityThread.java:3588)
      at android.app.ActivityThread.access$1200 (ActivityThread.java:165)
      at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1404)
      at android.os.Handler.dispatchMessage (Handler.java:102)
      at android.os.Looper.loop (Looper.java:150)
      at android.app.ActivityThread.main (ActivityThread.java:5621)
      at java.lang.reflect.Method.invoke (Method.java)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:794)
      at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:684)

1 个答案:

答案 0 :(得分:0)

您尝试过这种全球解决方案吗?

    public class MyApp extends Application {

    private static MyApp mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized MyApp getInstance() {
        return mInstance;
    }
}