ClassCastException when trying to do readParcelable on custom object array
Asked Answered
B

2

9

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?

Bobolink answered 3/12, 2013 at 14:17 Comment(0)
A
19

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

Read

tokens = in.createTypedArray(Tokens.CREATOR);

Write

in.writeTypedArray(tokens, flags);
Alysonalysoun answered 3/12, 2013 at 14:55 Comment(3)
That did the trick! Thanks a bunch, good way to end the day! :DBobolink
I did this and it works, thanks. I am just confused on what readParcelable is for if createTypedArray is actually for reading parcelables!Orthoepy
@Orthoepy I believe readParcelable is for reading a single valueAlysonalysoun
W
1

try this...

replace

 tokens = in.readParcelable(Tokens.class.getClassLoader();

with

 tokens = in.readParcelableArray(Tokens.class.getClassLoader());

you are writing Parcelable Array but reading it as Parcelable

Welter answered 3/12, 2013 at 14:40 Comment(3)
I tried that but that won't let me compile, I get "Type mismatch: cannot convert from Parcelable[] to Tokens[]" @Gopal RaoBobolink
@Bobolink add cast to Tokens[].... like tokens = (Tokens[])in.readParcelableArray(Tokens.class.getClassLoader());Welter
@ Gopal Rao I actually tried that aswell, and while that allows me to compile and run the app still crashes, with the error in my original post. "12-03 15:52:45.978: E/AndroidRuntime(6672): Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.example.incentive.messagemodel.Tokens[]"Bobolink

© 2022 - 2024 — McMap. All rights reserved.