Parcelable 协议需要一个名为 CREATOR 的 Parcelable.Creator 对象(我确实有 CREATOR)

2022-09-01 17:06:07

我正在尝试将可包裹数据从一个意图传递到另一个意图,这是我得到的错误:

08-31 14:12:22.709: E/AndroidRuntime(9931): FATAL EXCEPTION: main
08-31 14:12:22.709: E/AndroidRuntime(9931): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matejsoftware.cardscoretracker/com.matejsoftware.cardscoretracker.Igra}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.matejsoftware.cardscoretracker.Novaigra$Player

问题是:我确实有Parcellable.Creator对象。我将在下面发布整个可包裹代码:

public class Player implements Parcelable{
    String name;
    int score;
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(score);
    }

    public Player(Parcel source){
        score = source.readInt();
        name = source.readString();
    }

    public Player(int score, String name){
        this.score = score;
        this.name = name;
    }
}

public class MyCreator implements Parcelable.Creator<Player> {

    @Override
    public Player createFromParcel(Parcel source) {
        return new Player(source);
    }

    @Override
    public Player[] newArray(int size) {
        return new Player[size];
    }

}

造物主有什么问题吗?一旦我单击按钮开始下一个活动,应用程序就会崩溃。

这就是我在第二个活动中“检索”可包裹数据的方式:

//global variable
ArrayList<Player> playersParceledData;

//This is in onCreate
playersData = getIntent();
playersParceledData = playersData.getParcelableArrayListExtra("parceledData");

另外,这就是我如何将类对象放入ParcellableArrayListExtra:

Player newPlayer = new Player(0, text);
playersParceledData.add(newPlayer);
zacniIgro.putParcelableArrayListExtra("parceledData", playersParceledData);

答案 1

从包裹中读取时,您的顺序与您写入的顺序不同。

在你首先把 String,但在 中,你首先读取整数。这不是正确的方法,因为顺序或读/写很重要。writeToParcel()HeatFriendDetail(Parcel in)

以下是在向包裹写入/读取数据时正确排序的代码(另请参阅此链接):

public class FriendDetail implements Parcelable {

    private String full_name;
    private int privacy;

    public HeatFriendDetail(Parcel in) {
        this.full_name = in.readString();
        this.privacy = in.readInt();
    }

    public HeatFriendDetail() {

    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(this.full_name);
        dest.writeInt(this.privacy);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public HeatFriendDetail createFromParcel(Parcel in) {
            return new HeatFriendDetail(in);
        }

        public HeatFriendDetail[] newArray(int size) {
            return new HeatFriendDetail[size];
        }
    };

    // GETTER SETTER//
}

答案 2

我只在发布apk中收到此错误,因为创作者不是公开的。

我改变了这个:

static final Parcelable.Creator<Station> CREATOR = new Parcelable.Creator<Station>() {

对此:

public static final Parcelable.Creator<Station> CREATOR = new Parcelable.Creator<Station>() {

推荐