4

我在用于 android 应用程序的一些 Parcelable 自定义类中遇到了问题,我设法以一种非常奇怪的方式解决了这个问题。

仅在少数特定情况下从 parcelable 读取时发生崩溃(这使我认为我的实现并非完全错误)。

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

这是我在(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);
}

}

我设法通过更改某些 Parcelable 属性的读/写顺序来解决这个问题。我现在正在 cover_urls 之前读/写 where_to_buy 和 where_to_deliver,这似乎已经解决了这个问题。

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

(writeToParcel中的顺序是一样的,我这里只是省了几行,不粘贴)。

这个奇怪的修复就像一个魅力,我不知道为什么。

有没有人遇到过类似的事情或知道为什么打包/拆包的顺序会影响结果?

干杯!

4

1 回答 1

7

这不是异常行为。如果你了解它在内部是如何工作的,你就会明白为什么会出现异常。

序列化基本上是按顺序写入字节。因此,您正在以某种顺序(例如 4-16-1-2)写入一些可变长度的字节,然后您正在从Parcel.

从 读取时Parcel,您尝试读取例如 16 个字节,但实际上已写入 4 个字节,这就是发生崩溃的原因。

读取的顺序应该与写入的顺序相匹配,否则当您尝试读取例如 16 个字节时,您最终会遇到这种情况,但实际上您的变量是 4 个字节。

于 2017-05-05T11:03:56.927 回答