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

时间:2013-08-31 12:20:07

标签: java android eclipse parcelable

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

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代码:

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有什么不对吗?单击按钮开始下一个活动时,应用程序崩溃。

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

//global variable
ArrayList<Player> playersParceledData;

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

此外,这是我将类对象放到ParcelableArrayListExtra的方式:

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

6 个答案:

答案 0 :(得分:31)

从Parcel阅读时,您的序列与您写入的序列不同。

writeToParcel()中,您首先放置String,但在HeatFriendDetail(Parcel in)中,您首先读取整数。这不是正确的方法,因为订单或读/写很重要。

以下是在向/从Parcel写入/读取数据时执行正确顺序的代码(另请参阅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//
}

答案 1 :(得分:6)

我仅在发布apk中收到此错误,因为CREATOR不公开。

我改变了这个:

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

对此:

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

答案 2 :(得分:3)

刚碰到这个。

我有预感,我查看了我的ProGuard映射文件。

通常我会像这样声明CREATOR:

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

显示在映射文件中,如下所示:

    android.os.Parcelable$Creator CREATOR -> CREATOR

..除了一个类,报告错误的类。我宣布这样:

    public static Creator<ClassB> CREATOR =
            new Creator<ClassB>() {

瞧瞧:

    android.os.Parcelable$Creator CREATOR -> a

因此,如果您在生产版本中收到此异常,请检查您的映射文件。我认为ProGuard在决定不混淆CREATOR时非常敏感。

答案 3 :(得分:2)

当CREATOR类不是静态时,我收到此错误消息

答案 4 :(得分:1)

如果您使用的是Proguard规则,请将此行添加到您的proguard-rules.pro

-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;

}

答案 5 :(得分:-1)

试试这种方式

// create List of player
        ArrayList<HashMap<String, String>> players = new ArrayList<HashMap<String, String>>();

        //create Player

        HashMap<String, String> player = new HashMap<String, String>();
        player.put("score", "20");//replace 20 with your score variable
        player.put("name", "Biraj");//replace Biraj with your name variable

        // Add to arraylist

        players.add(player);

        // pass to intent

        Intent i;
        i.putExtra("PLAYERS", players);

        // read From intent

        ArrayList<HashMap<String, String>> playerFromintent = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("PLAYERS");

        // read from ArrayList

        for (HashMap<String, String> hashMap : playerFromintent) {
            System.out.println("Name : " + hashMap.get("name"));
            System.out.println("Score : " + hashMap.get("score"));
        }