异常描述
在一次序列化过程中,出现下面的报错:
** java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zhonghong.mycar/com.example.mycar.MyCarActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@e1c3112: Unmarshalling unknown type code 35 at offset 276
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2925)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3060)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1820)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6683)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@e1c3112: Unmarshalling unknown type code 35 at offset 276
at android.os.Parcel.readValue(Parcel.java:2747)
at android.os.Parcel.readSparseArrayInternal(Parcel.java:3118)
at android.os.Parcel.readSparseArray(Parcel.java:2351)
at android.os.Parcel.readValue(Parcel.java:2725)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3037)
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:288)
at android.os.BaseBundle.unparcel(BaseBundle.java:232)
at android.os.Bundle.getSparseParcelableArray(Bundle.java:1010)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1204)
at android.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1576)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1637)
at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3046)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2993)
at android.app.FragmentController.dispatchCreate(FragmentController.java:171)
at android.app.Activity.onCreate(Activity.java:1047)
at com.zhonghong.mycar.MyCarActivity.onCreate(MyCarActivity.java:36)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2905)
… 11 more**
在通过实现 Parcelable
接口自定义一个可以序列化的数据类,在传递给另一组件进行反序列化获取数据的过程中,出现了上诉异常,程序奔溃退出。
源码分析
Parcel.java 源码中,readValue()
方法从Native读取对象类型时,当类型不在匹配的值范围,匹配不到String、Integer、Map等等类型时,就会抛出此异常:throw new RuntimeException("Parcel " + this + ":Unmarshalling unknown type code " + type + " at offset " + off)
。
/**
* Read a typed object from a parcel. The given class loader will be
* used to load any enclosed Parcelables. If it is null, the default class
* loader will be used.
*/
@Nullable
public final Object readValue(@Nullable ClassLoader loader) {
int type = readInt();
switch (type) {
case VAL_NULL:
return null;
case VAL_STRING:
return readString();
case VAL_INTEGER:
return readInt();
case VAL_MAP:
return readHashMap(loader);
case VAL_PARCELABLE:
return readParcelable(loader);
...代码省略
default:
int off = dataPosition() - 4;
throw new RuntimeException(
"Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
}
}
通过 readInt()
方法根据Native地址读取对象类型:
/**
* Read an integer value from the parcel at the current dataPosition().
*/
public final int readInt() {
return nativeReadInt(mNativePtr);
}
在序列化 writeValue()
方法中写入对象类型:
public final void writeValue(@Nullable Object v) {
if (v == null) {
writeInt(VAL_NULL);
} else if (v instanceof String) {
writeInt(VAL_STRING);
writeString((String) v);
} else if (v instanceof Integer) {
writeInt(VAL_INTEGER);
writeInt((Integer) v);
} else if (v instanceof Map) {
...代码省略
} else {
Class<?> clazz = v.getClass();
if (clazz.isArray() && clazz.getComponentType() == Object.class) {
// Only pure Object[] are written here, Other arrays of non-primitive types are
// handled by serialization as this does not record the component type.
writeInt(VAL_OBJECTARRAY);
writeArray((Object[]) v);
} else if (v instanceof Serializable) {
// Must be last
writeInt(VAL_SERIALIZABLE);
writeSerializable((Serializable) v);
} else {
throw new RuntimeException("Parcel: unable to marshal value " + v);
}
}
}
writeArray()
、writeSparseArray()
、writeMap()
等等方法都调用了 writeValue()
,需要特别注意数据结构类型的序列化过程。
在创建序列化类的 writeToParcel()
过程中, writeString()
、writeFloat()
、writeLong()
等等方法不会写入类型值,因为 int 、float、long 等等是基本数据类型,不会创建对象,而对象数据类型是对基本数据类型的包装,创建了数据对象,引用了存储数值的地址。
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mId);
dest.writeFloat(mScore);
dest.writeString(mMessage);
}
案例分析
如下是出现异常的代码:
public static final @android.annotation.NonNull Parcelable.Creator<UserData> CREATOR =
new Parcelable.Creator<UserData>() {
@Override
public UserData createFromParcel(Parcel parcel) {
final String id = parcel.readString();
final String[] categoryIds = parcel.readStringArray();
final Bundle defaultArgs = parcel.readBundle();
final ArrayMap<String, String> categoryAlgorithms = new ArrayMap<>();
parcel.readMap(categoryAlgorithms, String.class.getClassLoader());
final ArrayMap<String, Bundle> categoryArgs = new ArrayMap<>();
parcel.readMap(categoryArgs, Bundle.class.getClassLoader());
final Builder builder = new Builder(id, categoryIds, categoryAlgorithms)
.setCategoryArgs()
.setBundle(defaultArgs);
return builder.build();
}
@Override
public UserData[] newArray(int size) {
return new UserData[size];
}
};
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeMap(mCategoryAlgorithms);
parcel.writeStringArray(mCategoryIds);
parcel.writeBundle(mDefaultArgs);
parcel.writeString(mId);
parcel.writeMap(mCategoryArgs);
}
其中 createFromParcel()
和 writeToParcel()
读和写的顺序和类型需要是一致的,不然序列化时获取的数据类型不一致,读取和写入的地址不一致则会解析出错抛出异常。
正确应该为:
...代码省略
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(mId);
parcel.writeStringArray(mCategoryIds);
parcel.writeBundle(mDefaultArgs);
parcel.writeMap(mCategoryAlgorithms);
parcel.writeMap(mCategoryArgs);
}
当出现此类异常时,检查 readValue()
和 writeParcel()
的顺序以及类型是否正确。特别是读写对象、数据结构时是否有误,其他数据类型也要顺序和类型读写一致。