错误处理Class not found when unmarshalling: android.support.v4.app.FragmentManagerState

一开始谷歌找遍了全网 , 直接搜这个报错貌似没有解决方法 T_T
希望这篇文章能够帮到各位啦.
解决方法拉到文末.

报错日志:

E/Parcel: Class not found when unmarshalling: android.support.v4.app.FragmentManagerState
    java.lang.ClassNotFoundException: android.support.v4.app.FragmentManagerState
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:309)
        at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
        at android.os.Parcel.readParcelable(Parcel.java:2245)
        at android.os.Parcel.readValue(Parcel.java:2152)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
        at android.os.BaseBundle.unparcel(BaseBundle.java:221)
        at android.os.Bundle.getParcelable(Bundle.java:755)
        at android.app.Activity.onCreate(Activity.java:932)  //可以看到是在onCreate的时候,进行恢复操作,导致Exception
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at com.duowan.gamecenter.pluginlib.a.abk.callActivityOnCreate(DelegateInstrumentation.java:239)
        at com.duowan.gamecenter.pluginlib.environment.abo.callActivityOnCreate(PluginInstrumentation.java:162)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

背景:
项目中的Activity是基于FragmentActivity, 当手机/模拟器的内存不足时(日志:E/History: low memory), 后台的活动被回收 ,只保留任务栈,再次进入的时候, Activity尝试恢复 , 出现该崩溃 .

FragmentActivity的onSaveInstanceState源码:

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        this.markFragmentsCreated();
        Parcelable p = this.mFragments.saveAllState();
        if (p != null) {
            outState.putParcelable("android:support:fragments", p);
        }

        if (this.mPendingFragmentActivityResults.size() > 0) {
            outState.putInt("android:support:next_request_index", this.mNextCandidateRequestIndex);
            int[] requestCodes = new int[this.mPendingFragmentActivityResults.size()];
            String[] fragmentWhos = new String[this.mPendingFragmentActivityResults.size()];

            for(int i = 0; i < this.mPendingFragmentActivityResults.size(); ++i) {
                requestCodes[i] = this.mPendingFragmentActivityResults.keyAt(i);
                fragmentWhos[i] = (String)this.mPendingFragmentActivityResults.valueAt(i);
            }

            outState.putIntArray("android:support:request_indicies", requestCodes);
            outState.putStringArray("android:support:request_fragment_who", fragmentWhos);
        }
    }

恢复的时候 , 会在super.onCreate(savedInstanceState);尝试恢复 , 导致出现本文的错误:Class not found when unmarshalling: android.support.v4.app.FragmentManagerState

解决思路就是处理savedInstanceState,不让它恢复android:support:fragments的内容.

解决方法如下:

在activity的onCreate移除掉Bundle 里的fragments

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            savedInstanceState.remove("android:support:fragments");
        }
        super.onCreate(savedInstanceState);
    }

你可能感兴趣的:(安卓问题解决,安卓bug修复,Class,not,found,when,unmarshalling:,unmarshalling,报错)