Android - Parcel: unable to marshal value

☆樱花仙子☆ 提交于 2019-12-24 16:16:40

问题


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?

Thanks


回答1:


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

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



来源:https://stackoverflow.com/questions/37862017/android-parcel-unable-to-marshal-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!