gpt4 book ai didi

java - 包含其自身的 ArrayList 的 Parcelable

转载 作者:行者123 更新时间:2023-12-01 11:16:02 24 4
gpt4 key购买 nike

我正在尝试将类别列表从一个 Activity 传递到另一个 Activity 。我已经在类上实现了 Parcelable,但每次都会使应用程序崩溃。注意:该 Activity 在未通过额外费用时有效。注2:我看不到任何日志,只是太慢了,突然“D/AndroidRuntime:正在关闭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);

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

关于java - 包含其自身的 ArrayList 的 Parcelable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31814013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com