android.os.BadParcelableException: ClassNotFoundException when unmarshalling

1.2k views Asked by At

We are facing getting a BadParceableException and we have no idea why. Above is the stacktrace:

    Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.my.app/br.com.my.app.commons.ui.MainActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: androidx.appcompat.widget.AppCompatSpinner$SavedState
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2789)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2872)
       at android.app.ActivityThread.-wrap11()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6543)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)

Caused by android.os.BadParcelableException: ClassNotFoundException when unmarshalling: androidx.appcompat.widget.AppCompatSpinner$SavedState
       at android.os.Parcel.readParcelableCreator(Parcel.java:2925)
       at android.os.Parcel.readParcelable(Parcel.java:2851)
       at android.view.AbsSavedState.<init>(AbsSavedState.java:67)
       at android.view.View$BaseSavedState.<init>(View.java:25139)
       at android.view.View$BaseSavedState.<init>(View.java:25128)
       at br.com.my.app.commons.ui.custom.DefaultOptionSpinner$SavedState.<init>(DefaultOptionSpinner.java:193)
       at br.com.my.app.commons.ui.custom.DefaultOptionSpinner$SavedState.<init>(DefaultOptionSpinner.java:185)
       at br.com.my.app.commons.ui.custom.DefaultOptionSpinner$SavedState$1.createFromParcel(DefaultOptionSpinner.java:206)
       at br.com.my.app.commons.ui.custom.DefaultOptionSpinner$SavedState$1.createFromParcel(DefaultOptionSpinner.java:204)
       at android.os.Parcel.readParcelable(Parcel.java:2860)
       at android.os.Parcel.readValue(Parcel.java:2754)
       at android.os.Parcel.readSparseArrayInternal(Parcel.java:3204)
       at android.os.Parcel.readSparseArray(Parcel.java:2416)
       at android.os.Parcel.readValue(Parcel.java:2811)
       at android.os.Parcel.readArrayMapInternal(Parcel.java:3123)
       at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)
       at android.os.BaseBundle.unparcel(BaseBundle.java:226)
       at android.os.Bundle.getSparseParcelableArray(Bundle.java:1009)
       at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:772)
       at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
       at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
       at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
       at androidx.fragment.app.FragmentManagerImpl.dispatchCreate(FragmentManagerImpl.java:2607)
       at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:235)
       at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:316)
       at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
       at br.com.zalf.prolog.commons.ui.MainActivity.onCreate(MainActivity.java:129)
       at android.app.Activity.performCreate(Activity.java:7009)
       at android.app.Activity.performCreate(Activity.java:7000)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2742)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2872)
       at android.app.ActivityThread.-wrap11()
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6543)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)

We saw some similars errors here in stackoverflow, but no one actually worked for us.

The exception is saying that the error happens at "super(in)", in this class:

private static class SavedState extends BaseSavedState { int mSelectedPosition;

SavedState(Parcelable superState) {
    super(superState);
}

private SavedState(android.os.Parcel in) {
    super(in);
    this.mSelectedPosition = in.readInt();
}

@Override
public void writeToParcel(android.os.Parcel out, int flags) {
    super.writeToParcel(out, flags);
    out.writeInt(this.mSelectedPosition);
}

public static final Parcelable.Creator<SavedState> CREATOR =
        new Parcelable.Creator<SavedState>() {
            public SavedState createFromParcel(android.os.Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };

}

Does anyone have any idea to help us?

Thanks!

1

There are 1 answers

0
Gustavo Navarro On BEST ANSWER

I found a solution. It is needed to indicate a bootloader as constructor parameter. I did it inside the second constructor:

private static class SavedState extends BaseSavedState {
        int mSelectedPosition;

        SavedState(Parcelable superState) {
            super(superState);
        }

        private SavedState(android.os.Parcel in) {
            super(in, DefaultOptionSpinner.class.getClassLoader());
            this.mSelectedPosition = in.readInt();
        }

        @Override
        public void writeToParcel(android.os.Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(this.mSelectedPosition);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {
                    public SavedState createFromParcel(android.os.Parcel in) {
                        return new SavedState(in);
                    }

                    public SavedState[] newArray(int size) {
                        return new SavedState[size];
                    }
                };
    }