Android-包裹:无法封送值
我正试图通过带有Parcelable的活动传递数据。这是我的代码:Android-包裹:无法封送值,android,parcelable,parcel,Android,Parcelable,Parcel,我正试图通过带有Parcelable的活动传递数据。这是我的代码: public class Player implements Parcelable { public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() { public Player createFromParcel(Parcel in) { return new
public class Player implements Parcelable {
public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
public Player createFromParcel(Parcel in) {
return new Player(in);
}
public Player[] newArray(int size) {
return new Player[size];
}
};
private String name;
private List<Card> listCards;
public Player(String namePlayer) {
name = namePlayer;
listCards = new ArrayList<>();
}
private Player(Parcel in) {
// This order must match the order in writeToParcel()
name = in.readString();
in.readList(listCards, null);
// Continue doing this for the rest of your member data
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeList(listCards);
}
你知道有什么问题吗
谢谢
由于
Card
类未实现Parcelable
,因此无法执行writeList/readList
。它仅适用于可包裹物品的列表。卡是一个单独的类别吗?这是否实现了Parcelable
?卡确实是一个单独的类,但它不实现Parcelable。它不仅适用于Parcelable,如果没有其他类型的对象可以被包裹,那么它会返回到写Parcelable-ed@cricket_007Serializable也将在writeToParcel
?writeList
循环所有对象并执行writeValue(对象)
-可在此处找到列表
java.lang.RuntimeException: Parcel: unable to marshal value com.antoinedelia.lebarbu_versionalcool.Card@ec54bb
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeList(listCards);
}