Android 可包裹订单
Android Parcelable order
我在 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.createFromParcel(TransportO ffer.java:43)
at com.worldcraze.worldcraze.API.Model.TransportOffer.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.createFromParcel(Ad.java:52)
at com.worldcraze.worldcraze.API.Model.Ad.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 属性的 reads/writes 的顺序解决了这个问题。
我现在 reading/writing where_to_buy 和 where_to_deliver 就在 cover_urls 之前,这似乎已经解决了问题。
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中的顺序是一样的,这里只是省了几行,没有粘贴)
这个奇怪的修复程序非常有效,我不知道为什么。
有没有人遇到过类似的事情或者知道为什么 packing/unpacking 的顺序会影响结果?
干杯!
这不是异常行为。如果您了解它的内部工作原理,您就会明白为什么会出现异常。
序列化基本上就是按顺序写入字节。因此,您正在以某种顺序(例如 4-16-1-2)写入一些可变长度的字节,然后您正在从 Parcel
中读取字节。
在阅读 Parcel
时,您尝试阅读例如16个字节,但你实际上写了4个字节,这就是崩溃的原因。
阅读的顺序应该与写作的顺序相匹配,否则当你试图阅读时,你会遇到这样的情况,例如16个字节,但实际上你的变量是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.createFromParcel(TransportO ffer.java:43)
at com.worldcraze.worldcraze.API.Model.TransportOffer.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.createFromParcel(Ad.java:52)
at com.worldcraze.worldcraze.API.Model.Ad.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 属性的 reads/writes 的顺序解决了这个问题。 我现在 reading/writing where_to_buy 和 where_to_deliver 就在 cover_urls 之前,这似乎已经解决了问题。
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中的顺序是一样的,这里只是省了几行,没有粘贴)
这个奇怪的修复程序非常有效,我不知道为什么。
有没有人遇到过类似的事情或者知道为什么 packing/unpacking 的顺序会影响结果?
干杯!
这不是异常行为。如果您了解它的内部工作原理,您就会明白为什么会出现异常。
序列化基本上就是按顺序写入字节。因此,您正在以某种顺序(例如 4-16-1-2)写入一些可变长度的字节,然后您正在从 Parcel
中读取字节。
在阅读 Parcel
时,您尝试阅读例如16个字节,但你实际上写了4个字节,这就是崩溃的原因。
阅读的顺序应该与写作的顺序相匹配,否则当你试图阅读时,你会遇到这样的情况,例如16个字节,但实际上你的变量是4个字节。