Android - 包裹 : unable to marshal value

标签 android parcelable parcel

我正在尝试使用 Parcelable 通过 Activity 传递数据。这是我的代码:

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);
}

每当我运行这段代码时,我都会收到错误:

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);
}

因为 Card 类没有实现 Parcelable,所以你不能执行 writeList/readList。它仅适用于 Parcelable 对象列表。

关于Android - 包裹 : unable to marshal value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37862017/

相关文章:

java - 如何正确实现 SparseArray 以在 ViewPager 的每个实例上存储自定义对象? Android 开发 - Java

android - Parcelable 接口(interface)保留对象引用? &Parcelable.CREATOR 是不是叫了?

reactjs - Typescript + Parcel new JSX transform - React is not defined 错误

java - 请求相机权限会导致 String.equals 中出现 NullPointerException

Android - 在不同的 Activity 中使用一个 DatePicker fragment

Kotlin 中的 Android Parcelable

android - Parcelable 中的标志有什么用?

android - 检测设备在线状态并发回服务器

没有clear()的Android map 更改瓦片覆盖

java - 如何在扩展 ImageView 的类上保存状态?