ClassNotFoundException when unmarshalling: android.support.v4.view.ViewPager$SavedState

喜你入骨 提交于 2019-11-29 05:25:28

I had the same error and here is what I did.

My issue came from using the ViewPager with a FragmentStatePagerAdapter. Inside one of the Fragments from the ViewPager had another ViewPager. Having that second view pager caused this rare bug. Even with or without an adapter on it.

The solution was to simply set setSaveEnabled(false); on the second ViewPager.

Ramanathan

Following solution worked for me:

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

}

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

If you don't want to include the source in v4 package you can declare the "fix" directly in your Adapter

    @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;
    }

In your Activity/Fragment, do the following:

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);
 //....
}

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

or another solution is:

map.onCreate(savedInstanceState);

to this:

map.onCreate(null);

Answer may be late.I used FragmentPagerAdapter instead of using FragmentStatePagerAdapter.Worked for me like a charm!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!