首页 > 解决方案 > 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());

标签: javaandroidarraylistparcelableparcel

解决方案


您应该使用其他方法来写入和读取Parcelables 列表:

  1. 用于writeTypedList写入Parcel

    parcel.writeTypedList(secondChildCategories);

  2. 用于createTypedArrayList阅读ParcelreadTypedList也可以使用)

    secondChildCategories = in.createTypedArrayList(SecondChildCategory.CREATOR);

希望有帮助。


推荐阅读