fragment来回切换导致crash IllegalStateException No activity moveToState

最近项目中遇到monkey错误。

 Line 113755: 05-14 23:53:06.860 E/AndroidRuntime( 7823): FATAL EXCEPTION: main
 Line 113757: 05-14 23:53:06.860 E/AndroidRuntime( 7823): Process: com.aliyun.dvrapp, PID: 7823
 Line 113759: 05-14 23:53:06.860 E/AndroidRuntime( 7823): java.lang.IllegalStateException: No activity
 Line 113761: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
 Line 113763: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1054)
 Line 113765: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1895)
 Line 113767: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.Fragment.performActivityCreated(Fragment.java:2083)
 Line 113769: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
 Line 113771: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1072)
 Line 113773: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.BackStackRecord.run(BackStackRecord.java:852)
 Line 113775: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
 Line 113777: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
 Line 113779: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.os.Handler.handleCallback(Handler.java:815)
 Line 113781: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.os.Handler.dispatchMessage(Handler.java:104)
 Line 113783: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.os.Looper.loop(Looper.java:192)
 Line 113785: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at android.app.ActivityThread.main(ActivityThread.java:5632)
 Line 113787: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at java.lang.reflect.Method.invoke(Native Method)
 Line 113789: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at java.lang.reflect.Method.invoke(Method.java:372)
 Line 113791: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
 Line 113793: 05-14 23:53:06.860 E/AndroidRuntime( 7823):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)

后来自己验证发现,不断的切换我写的2个fragment就会出现这个错误。

而我自己写的切换代码如下:

    public void openStorageFragment() {
        if (DVRStaticUtils.testIsSdPath() == -1) {
            Toast.makeText(this, R.string.noSdcard, Toast.LENGTH_SHORT).show();
            return;
        }
        if (storageFragment == null) {
            storageFragment = new StorageFragment();
        }
        isNowStorage = true;
        android.app.FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.replace(R.id.content_frame, storageFragment, "storage").commit();
      //  settingFragment = null; 原来没有,解决问题加的。
    }

    public void openSettingFragment() {
        if (settingFragment == null) {
            settingFragment = new SettingFragment();
        }
        isNowStorage = false;
        android.app.FragmentManager fragmentManager = getFragmentManager();
        android.app.FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.replace(R.id.content_frame, settingFragment, "setting").commit();
       // storageFragment = null;原来没有,解决问题加的。
    }

 所以,后来解决方法也在//注释这两行。很多人可能会担心内存溢出,直接赋值null是不是不好。由于我的fragment相对简单,经过50-100次测试,没有内存溢出。

不用太担心。

网上很多方法,修改commit为commitAllowingStateLoss()或者修改onDetech()也不太合适。






你可能感兴趣的:(fragment来回切换导致crash IllegalStateException No activity moveToState)