java - java Parcel@e7a33b1:解组未知类型的arraylist
问题描述
我正在尝试使用Parcelable
在android中传递数据,但是当我以正确的类型使用它时,我在这一行中遇到了错误,我不知道我在这里错过了什么。
这是在打包时出现问题的对象:
ArrayList<SecondChildCategory> secondChildCategories;
public ArrayList<SecondChildCategory> getSecondChildCategories() {
return secondChildCategories;
}
public void setSecondChildCategories(ArrayList<SecondChildCategory> secondChildCategories) {
this.secondChildCategories = secondChildCategories;
}
这里是用于读取数据的包裹构造函数:
protected ChildCategory(Parcel in) {
if (in.readByte() == 0) {
id = null;
} else {
id = in.readInt();
}
image = in.readString();
softDelete = in.readString();
if (in.readByte() == 0) {
productCategoryId = 0;
} else {
productCategoryId = in.readInt();
}
createdAt = in.readString();
updatedAt = in.readString();
parentCategoryID = in.readString();
backgroundColor = in.readString();
name = in.readString();
secondChildCategories = in.readArrayList(SecondChildCategory.class.getClassLoader()); // error reported here
hasChild = in.readByte() != 0;
}
在这里我是如何写的:
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(image);
parcel.writeString(softDelete);
parcel.writeInt(productCategoryId);
parcel.writeString(createdAt);
parcel.writeString(updatedAt);
parcel.writeString(parentCategoryID);
parcel.writeString(backgroundColor);
parcel.writeString(name);
parcel.writeList(secondChildCategories);
parcel.writeByte((byte) (hasChild ? 1 : 0));
}
我收到一个错误:
引起:java.lang.RuntimeException: Parcel android.os.Parcel@e7a33b1: Unmarshalling unknown type code 7143535 at offset 372
在这行代码上:
secondChildCategories = in.readArrayList(SecondChildCategory.class.getClassLoader());
解决方案
您应该使用其他方法来写入和读取Parcelable
s 列表:
用于
writeTypedList
写入Parcel
parcel.writeTypedList(secondChildCategories);
用于
createTypedArrayList
阅读Parcel
(readTypedList
也可以使用)secondChildCategories = in.createTypedArrayList(SecondChildCategory.CREATOR);
希望有帮助。
推荐阅读
- python - 在数据框列中选择非重复值
- excel - VBA宏打开/保存/关闭文件夹和子文件夹中的工作簿
- javascript - 在不知道加密方法的情况下,通过给定的密码和盐对用户进行身份验证
- python - 具有多值(概率)函数的神经网络回归
- mysql - 在非主键之间创建外键关系时出错
- scala - Scala,couchbase - 将 AsyncN1qlQueryResult 转换为自定义对象
- polymorphism - 如何使用可选参数对在 ReasionML/BuckleScript 中生成 Js.t 对象的函数进行 curry?
- javascript - 如何在 Bootsrap3 非真实链接()上禁用链接()上的 Javascript 操作?
- node.js - Mongoose,试图从同一个数据库填充,但不同的项目
- jquery - 如何防止在 mvc 中的 @Html.DropDownListFor 中多次选择相同的值