问题
I have a parent class called OuterClass
, which contains a List
of objects of type InnerClass
, which is a static inner class of OuterClass
. The static inner class implements Parcelable
.
I am running into this error:
Caused by android.os.BadParcelableException
ClassNotFoundException when unmarshalling: com.example.OuterClass.InnerClass
android.os.Parcel.readParcelableCreator + 2535 (Parcel.java:2535)
android.os.Parcel.readParcelable + 2461 (Parcel.java:2461)
android.os.Parcel.readValue + 2364 (Parcel.java:2364)
android.os.Parcel.readListInternal + 2778 (Parcel.java:2778)
android.os.Parcel.readArrayList + 2035 (Parcel.java:2035)
android.os.Parcel.readValue + 2385 (Parcel.java:2385)
android.os.Parcel.readArrayMapInternal + 2717 (Parcel.java:2717)
android.os.BaseBundle.unparcel + 269 (BaseBundle.java:269)
android.os.BaseBundle.getLongArray + 1271 (BaseBundle.java:1271)
android.content.Intent.getLongArrayExtra + 6441 (Intent.java:6441)
com.example.MyFragment.onCreate + 169 (MyFragment.java:169)
androidx.fragment.app.Fragment.performCreate + 2414 (Fragment.java:2414)
androidx.fragment.app.FragmentManagerImpl.moveToState + 1418 (FragmentManagerImpl.java:1418)
androidx .fragment.app.FragmentManagerImpl.moveFragmentToExpectedState + 1784 (FragmentManagerImpl.java:1784)
androidx.fragment.app.FragmentManagerImpl.moveToState + 1852 (FragmentManagerImpl.java:1852)
androidx. fragment.app.FragmentManagerImpl.dispatchStateChange + 3269 (FragmentManagerImpl.java:3269)
androidx.fragment.app.FragmentManagerImpl.dispatchCreate + 3223 (FragmentManagerImpl.java:3223)
androidx.fragment.app.FragmentController.dispatchCreate + 190 (FragmentController.java:190)
androidx.fragment.app.FragmentActivity.onCreate + 369 (FragmentActivity.java:369)
androidx.appcompat.app.AppCompatActivity.onCreate + 85 (AppCompatActivity.java:85)
com.example.MyBaseActivity.onCreate + 50 (MyBaseActivity.java:50)
android.app.Activity.performCreate + 6757 (Activity.java:6757)
android.app.Instrumentation.callActivityOnCreate + 1119 (Instrumentation.java:1119)
android.app.ActivityThread.performLaunchActivity + 2704 (ActivityThread.java:2704)
android.app.ActivityThread.handleLaunchActivity + 2812 (ActivityThread.java:2812)
android.app.ActivityThread.-wrap12 (ActivityThread.java)
android.app.ActivityThread$H.handleMessage + 1528 (ActivityThread.java:1528)
android.os.Handler.dispatchMessage + 102 (Handler.java:102)
android.os.Looper.loop + 154 (Looper.java:154)
android.app.ActivityThread.main + 6317 (ActivityThread.java:6317)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 872 (ZygoteInit.java:872)
com.android.internal.os.ZygoteInit.main + 762 (ZygoteInit.java:762)
This crash isn't happening for all users. My stack trace is actually very similar to the one in this post: How to find source of crashes of type java.lang.RuntimeException: Parcel android.os.Parcel@####: Unmarshalling unknown type code XXXX at offset YYY
The weird thing is, the unmarshalling part of the code is not even being executed during runtime, yet I still see this error.
public class OuterClass
{
List<InnerClass> innerClassList;
...
public static class InnerClass implements Parcelable
{
final String name;
final String value;
public InnerClass (Parcel in)
{
this.name = in.readString();
this.value = in.readString();
}
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(name);
dest.writeString(value);
}
public static Creator<InnerClass> CREATOR = new Creator<InnerClass>()
{
@Override
public InnerClass createFromParcel(Parcel source)
{
return new InnerClass(source);
}
@Override
public InnerClass[] newArray(int size)
{
return new InnerClass[size];
}
}
}
The inner class is passed between activities/fragments like this:
intent.putParcelableArrayListExtra(INNER_CLASS_LIST, innerClassList);
innerClassList = getArguments().getParcelableArrayList(INNER_CLASS_LIST);
The crash is being triggered on a seemingly unrelated line of code:
myIds = intent.getLongArrayExtra(EXTRA_MY_IDS)
// the intent object also contains a list of InnerClass
This internally calls BaseBundle.unparcel()
, which is then causing the BadParcelableException.
I must be missing something. Any help would be appreciated.
来源:https://stackoverflow.com/questions/58649428/android-os-badparcelableexception-classnotfoundexception-when-unmarshalling-inn