Search code examples
androidclasscastexceptionparcelableparcel

Error with unpacking a Custom Data Type from a Parcel in Android


I have this problem of recreating an array of a custom data type after it was written to a parcel.

playerwear is declared as such:

private Wearable playerWear[] = new Wearable[5];

This is the lines that write it to a parcel in the parent class. It is an array of type Wearable with 5 elements

        parcel.writeArray(playerWear);

This is the line that reads it from the Parcel (temp is an empty data element of the parent and has all elements filled in the same fashion as bellow. All others are working)

        temp.playerWear = (Wearable[])source.readArray(Wearable.class.getClassLoader());

Here is the code of the Parcel Interface that are defined in the Wearable datatype:

    @Override
public int describeContents()
{
    return 0;
}

/**
 * This is the main method of the Parcelable Interface.  This packs everything
 * up into the parcel that will be used in the next activity
 * @param parcel the parcel that will be passed to the next activity
 * @param i used to keep track of the size...I think
 */
@Override
public void writeToParcel(Parcel parcel, int i)
{
    parcel.writeString(slotName);
    parcel.writeParcelable(heldItem, i);
    if (holdingItem)
        parcel.writeString("True");
    else
        parcel.writeString("False");
}

/**
 * This is the method that takes the passed parcel and rebuilds it so that it can be used
 * @param source the passed parcel
 * @return the reconstructed data type
 */
public static Wearable recreateParcel(Parcel source)
{
    Wearable temp = new Wearable();
    temp.slotName = source.readString();
    temp.heldItem = (Item) source.readParcelable(Item.class.getClassLoader());
    String bool = source.readString();
    if(bool.equals("True"))
        temp.holdingItem = true;
    else
        temp.holdingItem = false;

    return temp;
}

/**
 * Not truly sure what this is but I think this is similar to the classLoader
 */
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{
    /**
     * Calls the method that I created to recreate the Data type
     * @param in the passed parcel
     * @return s the return of the recreateParcel method
     */
    public Wearable createFromParcel(Parcel in)
    {
        return recreateParcel(in);
    }

    /**
     * Called if the custom data type is in an array
     * @param size the size of the array. Used to figure out how many elements are needed
     * @return an Array of the Data type
     */
    public Wearable[] newArray(int size)
    {
        return new Wearable[size];
    }
};

Lastly this is the error that it is giving me,

Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.rtbd.storytime.Wearable[]

Please post if I forgot any vital information that is needed to solve this problem.

Thanks!


Solution

  • After doing a lot more research I found the answer at: Read & writing arrays of Parcelable objects

    You don't want to write as a parcelableArray or as a Array you want to write it as a TypedArray and then call the CREATOR of the data type in the createTypedArray method.