Android - 包裹:无法编组价值 [英] Android - Parcel: unable to marshal value

查看:87
本文介绍了Android - 包裹:无法编组价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to pass data through activities with Parcelable. This is my code :

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

每当我运行此代码时,都会出现错误:

Whenever I run this code, I get the error :

java.lang.RuntimeException: Parcel: unable to marshal value com.antoinedelia.lebarbu_versionalcool.Card@ec54bb

您知道可能是什么问题吗?

Do you have any idea what could be the problem?

谢谢

推荐答案

@Override
public void writeToParcel(Parcel dest, int flags) {
  dest.writeString(name);
  dest.writeList(listCards);
}

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

Since Card class does not implement Parcelable you cannot do writeList/readList. It is only applicable for List of Parcelable Objects.

这篇关于Android - 包裹:无法编组价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆