34

我在我的 Android 崩溃报告中看到以下错误:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v4.view.ViewPager$SavedState
at android.os.Parcel.readParcelable(Parcel.java:1971)
at android.os.Parcel.readValue(Parcel.java:1859)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:2128)
at android.os.Parcel.readSparseArray(Parcel.java:1581)
at android.os.Parcel.readValue(Parcel.java:1916)
at android.os.Parcel.readMapInternal(Parcel.java:2099)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:1225)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:806)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:160)
at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1539)
at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1422)
at android.view.ViewGroup.drawChild(ViewGroup.java:3028)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.ViewGroup.drawChild(ViewGroup.java:3184)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.ViewGroup.drawChild(ViewGroup.java:3184)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.ViewGroup.drawChild(ViewGroup.java:3184)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2788)
at android.view.View.draw(View.java:11017)
at android.widget.FrameLayout.draw(FrameLayout.java:450)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2175)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2234)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1810)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2695)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4977)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

恢复活动时,它似乎间歇性地发生。我不会在任何代码中直接访问 SavedState 类。

4

5 回答 5

27

我有同样的错误,这就是我所做的。

我的问题来自将 ViewPager 与 FragmentStatePagerAdapter 一起使用。ViewPager 中的一个片段内部有另一个 ViewPager。拥有第二个视图寻呼机导致了这个罕见的错误。即使有或没有适配器。

解决方案是简单地设置 setSaveEnabled(false); 在第二个 ViewPager 上。

于 2012-09-17T20:45:11.943 回答
14

以下解决方案对我有用:

package android.support.v4.app;

import android.os.Bundle;
import android.view.ViewGroup;

public abstract class FixedFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

  public FixedFragmentStatePagerAdapter(FragmentManager fm) {
    super(fm);
  }

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    Fragment f = (Fragment)super.instantiateItem(container, position);
    Bundle savedFragmentState = f.mSavedFragmentState;
    if (savedFragmentState != null) {
      savedFragmentState.setClassLoader(f.getClass().getClassLoader());
    }
    return f;
  }

}

来自http://code.google.com/p/android/issues/detail?id=37484#c1

于 2012-12-20T16:34:46.407 回答
8

如果您不想在 v4 包中包含源代码,您可以直接在适配器中声明“修复”

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final Object fragment = super.instantiateItem(container, position);
        try {
            final Field saveFragmentStateField = Fragment.class.getDeclaredField("mSavedFragmentState");
            saveFragmentStateField.setAccessible(true);
            final Bundle savedFragmentState = (Bundle) saveFragmentStateField.get(fragment);
            if (savedFragmentState != null) {
                savedFragmentState.setClassLoader(Fragment.class.getClassLoader());
            }
        } catch (Exception e) {
            Log.w("CustomFragmentStatePagerAdapter", "Could not get mSavedFragmentState field", e);
        }
        return fragment;
    }
于 2013-12-25T14:54:17.160 回答
8

在您的活动/片段中,执行以下操作:

public void onSaveInstanceState(Bundle outState){
     //This MUST be done before saving any of your own or your base class's     variables
    final Bundle mapViewSaveState = new Bundle(outState);
    mapView.onSaveInstanceState(mapViewSaveState);
    outState.putBundle("mapViewSaveState", mapViewSaveState);
   //Add any other variables here.
   super.onSaveInstanceState(outState);
}

public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   final Bundle mapViewSavedInstanceState = savedInstanceState != null ?      savedInstance.getBundle("mapViewSaveState") : null;
 mapView.onCreate(mapViewSavedInstanceState);
 //....
}

来源:https ://code.google.com/p/gmaps-api-issues/issues/detail?id=6237

或另一种解决方案是:

map.onCreate(savedInstanceState);

对此:

map.onCreate(null);

于 2016-09-16T06:34:53.973 回答
1

答案可能会迟到。我使用FragmentPagerAdapter而不是使用FragmentStatePagerAdapter。像魅力一样为我工作!

于 2014-10-16T21:24:38.113 回答