Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 

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

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"); 

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

Player newPlayer = new Player(0, text); playersParceledData.add(newPlayer); zacniIgro.putParcelableArrayListExtra("parceledData", playersParceledData); 
like image 544
Guy Avatar asked Aug 31 '13 12:08

Guy


People also ask

What is a Parcelable object?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

How do you implement Parcelable?

Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.

What is a primary purpose of the Parcelable interface?

The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another. For this example you'll look at how to implement parcelable in a simple class.

Why do we need Parcelable in Android?

Parcelable and Bundle objects are intended to be used across process boundaries such as with IPC/Binder transactions, between activities with intents, and to store transient state across configuration changes.


1 Answers

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// } 
like image 112
Imtiyaz Khalani Avatar answered Sep 22 '22 06:09

Imtiyaz Khalani