Class not found when unmarshalling: android.support.v7.widget.Toolbar$SavedState

后端 未结 4 757
悲哀的现实
悲哀的现实 2020-12-31 12:09

i am using Maps API to create a simple android app and i get a wierd error i can\'t solve. It usually happens when i rotate my device. I\'m using google services 8.4.0

4条回答
  •  情歌与酒
    2020-12-31 13:03

    I had a similar problem with my custom view and found this solution. I noticed that this crash occurred when I extended RecyclerView or AppCompatSpinner and needed to save the state. The crash will probably happen for other views as well.

    Basically the crash is caused by a bug in AbsSavedState as mentioned here. And the crash only occurs when the constructor of the SavedState is called without a class loader. It seemed like this was an old issue however I started getting crash reports for Android 9 and 10.

    So the fix was to change:

    public SavedState(Parcel source) {
      super(source);
      //... 
    }
    

    to:

    public SavedState(Parcel source) {
      super(source, LinearLayoutManager.class.getClassLoader());
      //... 
    }
    

    Edit I was on the right track, but then I was looking at some Android code and found out that there was actually a constructor missing that caused the crash. So I had the following constructor for the SavedState:

    SavedState(Parcel in)
    {
        super(in);
        //...
    }
    

    And I needed to add the following:

    @RequiresApi(Build.VERSION_CODES.N)
    SavedState(Parcel in, ClassLoader loader)
    {
        super(in, loader);
        //...
    }
    

    And then I needed to change the creator:

    public static final Creator CREATOR = new ClassLoaderCreator()
    {
        @Override
        public SavedState createFromParcel(Parcel source, ClassLoader loader)
        {
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? new SavedState(source, loader) : new SavedState(source);
        }
    
        @Override
        public SavedState createFromParcel(Parcel source)
        {
            return createFromParcel(source, null);
        }
    
        public SavedState[] newArray(int size)
        {
            return new SavedState[size];
        }
    };
    

提交回复
热议问题