Android Parcelable order

I had a problem with some of the custom Parcelable classes that I use for an Android app, which I managed to solve in a very strange way.

I had a failure while reading from the detailed in only a few specific cases (which led me to think that my implementation was not completely wrong).

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.worldcraze.worldcraze/com.worldcraze.worldcraze.AdActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: Surface Book
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: android.os.BadParcelableException: ClassNotFoundException when     unmarshalling: Surface Book
at android.os.Parcel.readParcelableCreator(Parcel.java:2432)
at android.os.Parcel.readParcelable(Parcel.java:2358)
at com.worldcraze.worldcraze.API.Model.TransportOffer.<init>     (TransportOffer.java:33)
at     com.worldcraze.worldcraze.API.Model.TransportOffer$1.createFromParcel(TransportO    ffer.java:43)
    at     com.worldcraze.worldcraze.API.Model.TransportOffer$1.createFromParcel(TransportO    ffer.java:40)
at android.os.Parcel.createTypedArray(Parcel.java:2167)
at com.worldcraze.worldcraze.API.Model.Ad.<init>(Ad.java:42)
at com.worldcraze.worldcraze.API.Model.Ad$1.createFromParcel(Ad.java:52)
at com.worldcraze.worldcraze.API.Model.Ad$1.createFromParcel(Ad.java:49)
at android.os.Parcel.readParcelable(Parcel.java:2367)
at android.os.Parcel.readValue(Parcel.java:2264)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2614)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:786)
at android.content.Intent.getParcelableExtra(Intent.java:5377)
at com.worldcraze.worldcraze.AdActivity.onCreate(AdActivity.java:57)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
... 9 more

Here is my initial implementation of the Model that crashed (Ad.java)

public class Ad implements Parcelable {

public String              author;
public String              transporter;
public Image               cover_urls;
public String              details;
public String              id;
public int                 item_size;
public String              object_name;
public Money               tips;
public Money               price;
public Location            where_to_buy;
public String              resource_uri;
public Location            where_to_deliver;
public Permissions         permissions;
public TransportOffer[]    transport_offers;
public float               fees_lemonway_CB;
public String              validation_code;
public Date                creation_dtime;
public String              accepted_transport_offer_id;

protected   Ad(Parcel in) {
    author           = in.readString();
    transporter      = in.readString();
    details          = in.readString();
    id               = in.readString();
    item_size        = in.readInt();
    object_name      = in.readString();
    resource_uri     = in.readString();
    cover_urls       = in.readParcelable(Image.class.getClassLoader());
    tips             = in.readParcelable(Money.class.getClassLoader());
    price            = in.readParcelable(Money.class.getClassLoader());
    permissions      = in.readParcelable(Permissions.class.getClassLoader());
    transport_offers = in.createTypedArray(TransportOffer.CREATOR);
    where_to_buy     = in.readParcelable(Location.class.getClassLoader());
    where_to_deliver = in.readParcelable(Location.class.getClassLoader());
    fees_lemonway_CB = in.readFloat();
    validation_code  = in.readString();
    creation_dtime   = (Date) in.readSerializable();
    accepted_transport_offer_id = in.readString();
}

public static final Creator<Ad> CREATOR = new Creator<Ad>() {
    @Override
    public Ad createFromParcel(Parcel in) {
        return new Ad(in);
    }

    @Override
    public Ad[] newArray(int size) {
        return new Ad[size];
    }
};

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(author);
    dest.writeString(transporter);
    dest.writeString(details);
    dest.writeString(id);
    dest.writeInt(item_size);
    dest.writeString(object_name);
    dest.writeString(resource_uri);
    dest.writeParcelable(cover_urls, flags);
    dest.writeParcelable(tips, flags);
    dest.writeParcelable(price, flags);
    dest.writeParcelable(permissions, flags);
    dest.writeTypedArray(transport_offers, flags);
    dest.writeParcelable(where_to_buy, flags);
    dest.writeParcelable(where_to_deliver, flags);
    dest.writeFloat(fees_lemonway_CB);
    dest.writeString(validation_code);
    dest.writeSerializable(creation_dtime);
    dest.writeString(accepted_transport_offer_id);
}

}

I managed to fix the problem by changing the read / write order of some Parcelable attributes. Now I am reading / writing where_to_buy and where_to_deliver right before cover_urls, which seems to have fixed the problem.

    where_to_buy     = in.readParcelable(Location.class.getClassLoader());
    where_to_deliver = in.readParcelable(Location.class.getClassLoader());
    cover_urls       = in.readParcelable(Image.class.getClassLoader());
    tips             = in.readParcelable(Money.class.getClassLoader());
    price            = in.readParcelable(Money.class.getClassLoader());
    permissions      = in.readParcelable(Permissions.class.getClassLoader());
    transport_offers = in.createTypedArray(TransportOffer.CREATOR);

(the order is written in writeToParcel, I just save a few lines here without inserting it).

This weird fix works like a charm, and I have no idea why.

- - , / ?

!

+4
1

. , , , .

. , (, 4-16-1-2), Parcel.

Parcel, , . 16 , 4 , .

, , , . 16 , 4 .

+4

All Articles