ClassCastException when trying to do readParcelable on custom object array

Phoebe picture Phoebe · Dec 3, 2013 · Viewed 9.6k times · Source

I'm trying to implement Parcelable instead of Serializble as it's supposed to be more efficient. From my MainActivity I want to pass a Message object to another activity, my class Message contains other classes. If I get past this one error I'm sure I'll run in to a lot more of them, but right now when I try to run my app I'm getting

This is my Message-class:

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class Message implements Parcelable {

    int id;
    int space_id;
    String text;
    String html;
    String created_at;
    int[] comments;
    boolean is_liked;
    int[] liked_by;
    int[] starred_by;
    boolean is_starred;
    boolean is_followed;
    int[] followed_by;
    ArrayList<AttachmentsData> attachments;
    String url;
    int[] tags;
    boolean is_system;

    public User created_by;
    public Permissions persmissions;
    public Embed embed;
    public Tokens[] tokens;


    /** Parcelable **/
    public Message(Parcel in) {
        readFromParcel(in);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) { 
        dest.writeInt(id);
        dest.writeInt(space_id);
        dest.writeString(text);
        dest.writeString(html);
        dest.writeString(created_at);
        dest.writeIntArray(comments);
        dest.writeInt(is_liked ? 1 : 0);
        dest.writeIntArray(liked_by);
        dest.writeIntArray(starred_by);
        dest.writeInt(is_starred ? 1 : 0);
        dest.writeInt(is_followed ? 1 : 0);
        dest.writeIntArray(followed_by);
        dest.writeList(attachments);
        dest.writeString(url);
        dest.writeIntArray(tags);
        dest.writeInt(is_system ? 1 : 0);

        dest.writeParcelable(created_by, flags);
        dest.writeParcelable(persmissions, flags);
        dest.writeParcelable(embed, flags);
        dest.writeParcelableArray(tokens, flags);
    }

    private void readFromParcel(Parcel in) {
        id = in.readInt();
        space_id = in.readInt();
        text = in.readString();
        html = in.readString();
        created_at = in.readString();
        //in.readIntArray(comments);
        comments = in.createIntArray();
        is_liked = in.readInt() == 0;
        //in.readIntArray(liked_by);
        liked_by = in.createIntArray();
        //in.readIntArray(starred_by);
        starred_by = in.createIntArray();
        is_starred = in.readInt() == 0;
        is_followed = in.readInt() == 0;
        //in.readIntArray(followed_by);
        followed_by = in.createIntArray();
        in.readList(attachments, AttachmentsData.class.getClassLoader());
        url = in.readString();
        //in.readIntArray(tags);
        tags = in.createIntArray();
        is_system = in.readInt() == 0;

        created_by = in.readParcelable(User.class.getClassLoader());
        persmissions = in.readParcelable(Permissions.class.getClassLoader());
        embed = in.readParcelable(Embed.class.getClassLoader());
        tokens = in.readParcelable(Tokens.class.getClassLoader();
    }   

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

        public Message createFromParcel(Parcel in) {
            return new Message(in);
        }

        public Message[] newArray(int size) {
            return new Message[size];
        }
    };



    /** Getters and setters, I'll spare you these **/

 }

And this is the error I am getting:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.incentive/com.example.incentive.Comments}: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.example.incentive.messagemodel.Tokens[] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) at android.app.ActivityThread.access$600(ActivityThread.java:127) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4441) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.example.incentive.messagemodel.Tokens[] at com.example.incentive.messagemodel.Message.readFromParcel(Message.java:94) at com.example.incentive.messagemodel.Message.(Message.java:35) at com.example.incentive.messagemodel.Message$1.createFromParcel(Message.java:100) at com.example.incentive.messagemodel.Message$1.createFromParcel(Message.java:1) at android.os.Parcel.readParcelable(Parcel.java:1992) at android.os.Parcel.readValue(Parcel.java:1854) at android.os.Parcel.readMapInternal(Parcel.java:2094) at android.os.Bundle.unparcel(Bundle.java:223) at android.os.Bundle.getParcelable(Bundle.java:1158) at com.example.incentive.Comments.onCreate(Comments.java:67) at android.app.Activity.performCreate(Activity.java:4465) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) ... 11 more

I'm thinking the relevant one now is:

Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.example.incentive.messagemodel.Tokens[] at com.example.incentive.messagemodel.Message.readFromParcel(Message.java:94)

So I'm probably doing this one wrong: tokens = in.readParcelable(Tokens.class.getClassLoader();

Here is my Tokens-class:

import android.os.Parcel;
import android.os.Parcelable;

public class Tokens implements Parcelable {

    String type;
    String value;
    int start;
    int end;


    /** Parcelable related **/
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(type);
        dest.writeString(value);
        dest.writeInt(start);
        dest.writeInt(end);
    }

    private Tokens(Parcel in) {
        type = in.readString();
        value = in.readString();
        start = in.readInt();
        end = in.readInt();
    }

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

        public Tokens createFromParcel(Parcel in) {
            return new Tokens(in);
        }

        public Tokens[] newArray(int size) {
            return new Tokens[size];
        }
    };


    /** Getters and setters **/ 
}

I've searched all afternoon and I can't see to find how to do it properly. Does anyone know?

Answer

Matthew Mcveigh picture Matthew Mcveigh · Dec 3, 2013

Use writeTypedArray and readTypedArray to read and write your array to the parcel

Read

tokens = in.createTypedArray(Tokens.CREATOR);

Write

in.writeTypedArray(tokens, flags);