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

后端 未结 5 767
孤街浪徒
孤街浪徒 2020-12-10 10:09

I am seeing the following error in my Android crash reports:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling: android.support.v4         


        
相关标签:
5条回答
  • 2020-12-10 10:41

    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;
        }
    
    0 讨论(0)
  • 2020-12-10 10:48

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

    0 讨论(0)
  • 2020-12-10 10:56

    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

    0 讨论(0)
  • 2020-12-10 11:01

    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.

    0 讨论(0)
  • 2020-12-10 11:05

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

    0 讨论(0)
提交回复
热议问题