包含自身的 ArrayList 的 Parcelable

Parcelable containing an ArrayList of itself

我正在尝试将类别列表从 Activity 传递给另一个。我已经在 class 上实现了 Parcelable,但它每次都会使应用程序崩溃。注意: activity 在不传递额外内容时有效。 注2:看不到任何日志,就是这么慢突然"D/AndroidRuntime﹕ Shutting down VM"

这是包裹:

public  class   Category implements Parcelable{

private String categoryId;
private String name;
private boolean active;
private String slug;
private String externalId;
private String parentId;
private Integer level;
private String categoryTreeNodeId;
private ArrayList<Category> children;


public Category() {}

// Getters and Setters...

@Override
public int describeContents() {
    // ignore for now
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(categoryId);
    dest.writeString(name);
    dest.writeByte((byte) (active ? 1 : 0));
    dest.writeString(slug);
    dest.writeString(externalId);
    dest.writeString(parentId);
    dest.writeInt(level);
    dest.writeString(categoryTreeNodeId);
    dest.writeTypedList(children);
}

/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<Category> CREATOR = new Parcelable.Creator<Category>() {
    public Category createFromParcel(Parcel pc) {
        return new Category(pc);
    }
    public Category[] newArray(int size) {
        return new Category[size];
    }
};

/**Ctor from Parcel, reads back fields IN THE ORDER they were written */
public Category(Parcel in){
    categoryId = in.readString();
    name = in.readString();
    active = in.readByte() != 0;
    slug = in.readString();
    externalId = in.readString();
    parentId = in.readString();
    level =  in.readInt();
    categoryTreeNodeId = in.readString();
    in.readTypedList(children, Category.CREATOR);
}

}

这就是我对 activity 的称呼:

ArrayList<Category> list = currentCategory.getChildren();
    intent.putParcelableArrayListExtra(SubCategoryActivity.EXTRA_CATEGORY_LIST, list);
    intent.putExtra(SubCategoryActivity.EXTRA_CURRENT_CAT_NAME, currentCategory.getName());
    context.startActivity(intent);

EXTRA_CURRENT_CAT_NAME 只是一个字符串,它运行良好。

我认为问题出在 ArrayList 中。有做类似事情的建议或经验吗?

谢谢。

我得到的唯一日志是:

08-04 17:56:21.529 30543-30543/com.example.test D/AndroidRuntime﹕ Shutting down VM 08-04 17:56:41.554 30543-30552/com.example.test I/art﹕ Thread[5,tid=30552,WaitingInMainSignalCatcherLoop,Thread*=0xb81e22d0,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3 08-04 17:56:41.554 30543-30552/com.example.test I/art﹕ [ 08-04 17:56:41.754 787: 809 I/Process ] Sending signal. PID: 787 SIG: 3

您的问题是由于将键入的列表读入空容器引起的:

in.readTypedList(children, Category.CREATOR);

有点迷惑,但是readTypedList的用例要求传入的List参数是非空的。我建议将其更改为使用:

children = in.createTypedArrayList(Category.CREATOR);

如果列表在写入包裹时为空,这也将保持空值。