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

后端 未结 5 772
孤街浪徒
孤街浪徒 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;
        }
    

提交回复
热议问题