Parcelable protocol requires a Parcelable.Creator object called CREATOR (I do have CREATOR)

让人想犯罪 __ 提交于 2019-11-28 06:51:08
Imtiyaz Khalani

You are have different sequence when reading from Parcel than the one you write 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.

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

I received this error in release apk only, because the CREATOR was not public.

I changed this :

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

To this :

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

Just ran into this.

I had a hunch, and I looked in my ProGuard mapping file.

Normally I declare CREATOR like this:

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

which shows up in the mapping file like this:

    android.os.Parcelable$Creator CREATOR -> CREATOR

..except for one class, the class that was reported with the error. I declared it like this:

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

Lo and behold:

    android.os.Parcelable$Creator CREATOR -> a

So if you are getting this exception in your production release, check your mapping file. I think ProGuard is really sensitive to how CREATOR is declared when deciding not to obfuscate it.

I had this error message when the CREATOR class was not static

If you are using proguard rules add this line in to your proguard-rules.pro

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

}

Try this way

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