Parcelable协议需要一个名为CREATOR的Parcelable.Creator对象(我有CREATOR) [英] Parcelable protocol requires a Parcelable.Creator object called CREATOR (I do have CREATOR)

查看:1139
本文介绍了Parcelable协议需要一个名为CREATOR的Parcelable.Creator对象(我有CREATOR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to pass Parcelable data from one intent to another and this is the error I am getting:

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

事情是:我有Parcelable.Creator对象。我将在下面发布整个Parcelable代码:

The thing is: I do have Parcelable.Creator object. I'll post the whole Parcelable code below:

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

}

CREATOR有没有错?一旦我点击按钮开始下一个活动,应用程序就会崩溃。

Is there somethin wrong with the CREATOR? The app crashes as soon as I click the button to start the next activity.

这是我如何在第二个活动中检索包裹数据:

This is how I "retrieve" Parcelable data in the second activity:

//global variable
ArrayList<Player> playersParceledData;

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

另外,这是我如何将类对象放到ParcelableArrayListExtra中:

Also, this is how I put class objects to ParcelableArrayListExtra:

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


推荐答案

从Parcel阅读时,您的顺序与一个你写的。

You are have different sequence when reading from Parcel than the one you write in.

writeToParcel()你首先把String放在 HeatFriendDetail(Parcel in),你首先读取整数。这是不正确的方式,因为订单或读/写是重要的。

In writeToParcel() you are first putting String but in the HeatFriendDetail(Parcel in), you first read integer. It is not the correct way because the order or read/write matters.

以下是在向/从Parcel写入/读取数据时进行正确顺序的代码请参阅此链接):

Following is the code which makes correct order when writing/reading the data to/from Parcel (also see this link):

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//
}

这篇关于Parcelable协议需要一个名为CREATOR的Parcelable.Creator对象(我有CREATOR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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