我有两个名为AlacarteCategory
和AlacartePack
AlacartePack类
public class AlacartePack implements Parcelable {
private int alacartePackId;
private String alacartePackName;
private double price;
private String smsCode;// no need to pass
private Boolean isSelected;// no need to pass
private List<Channel> channelList;// no need to pass
public AlacartePack(){
channelList = new ArrayList<Channel>();
isSelected = false;
}
//// getter & setter
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int i) {
dest.writeString(alacartePackName);
dest.writeInt(alacartePackId);
dest.writeDouble(price);
}
private AlacartePack(Parcel in){
this.alacartePackId = in.readInt();
this.alacartePackName = in.readString();
this.price = in.readDouble();
}
public static final Parcelable.Creator<AlacartePack> CREATOR = new Parcelable.Creator<AlacartePack>() {
@Override
public AlacartePack createFromParcel(Parcel source) {
return new AlacartePack(source);
}
@Override
public AlacartePack[] newArray(int size) {
return new AlacartePack[size];
}
};
}
AlacarteCategory类
public class AlacarteCategory implements Parcelable {
private int categoryId;
private String categoryName;
private List<AlacartePack> alacartePackList;
public int getCategoryId() {
return categoryId;
}
public AlacarteCategory(){
alacartePackList = new ArrayList<AlacartePack>();
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public List<AlacartePack> getAlacartePackList() {
return alacartePackList;
}
public void setAlacartePackList(List<AlacartePack> alacartePackList) {
this.alacartePackList = alacartePackList;
}
public void addPack(AlacartePack mAlacartePack){
alacartePackList.add(mAlacartePack);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int i) {
dest.writeString(categoryName);
dest.writeInt(categoryId);
dest.writeTypedList(alacartePackList);
}
private AlacarteCategory(Parcel in){
this.categoryId = in.readInt();
this.categoryName = in.readString();
this.alacartePackList = in.readArrayList(AlacartePack.class.getClassLoader());
//in.readTypedList(alacartePackList,AlacartePack.CREATOR);
}
public static final Parcelable.Creator<AlacarteCategory> CREATOR = new Parcelable.Creator<AlacarteCategory>() {
@Override
public AlacarteCategory createFromParcel(Parcel source) {
return new AlacarteCategory(source);
}
@Override
public AlacarteCategory[] newArray(int size) {
return new AlacarteCategory[size];
}
};
}
现在我想用我的意图传递AlacarteCategory的ArrayList。它给出了解组问题:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{Dishtv.Dynamic/dishtv.dynamic.RechargePaymentActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@b5903448: Unmarshalling unknown type code 4980781 at offset 152
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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.RuntimeException: Parcel android.os.Parcel@b5903448: Unmarshalling unknown type code 4980781 at offset 152
at android.os.Parcel.readValue(Parcel.java:1921)
at android.os.Parcel.readListInternal(Parcel.java:2103)
at android.os.Parcel.readArrayList(Parcel.java:1544)
at dishtv.dynamic.model.AlacarteCategory.<init>(AlacarteCategory.java:66)
at dishtv.dynamic.model.AlacarteCategory.<init>(AlacarteCategory.java:12)
at dishtv.dynamic.model.AlacarteCategory$1.createFromParcel(AlacarteCategory.java:74)
at dishtv.dynamic.model.AlacarteCategory$1.createFromParcel(AlacarteCategory.java:70)
at android.os.Parcel.readParcelable(Parcel.java:1992)
at android.os.Parcel.readValue(Parcel.java:1854)
at android.os.Parcel.readListInternal(Parcel.java:2103)
at android.os.Parcel.readArrayList(Parcel.java:1544)
at android.os.Parcel.readValue(Parcel.java:1875)
at android.os.Parcel.readMapInternal(Parcel.java:2094)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getString(Bundle.java:1048)
at android.content.Intent.getStringExtra(Intent.java:3923)
at dishtv.dynamic.RechargePaymentActivity.onCreate(RechargePaymentActivity.java:52)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
我的代码有什么问题?
答案 0 :(得分:1)
问题是因为你的写作和阅读顺序不同。
例如,对于AlacartePack
类,方法writeToParcel
没问题:
@Override
public void writeToParcel(Parcel dest, int i) {
dest.writeString(alacartePackName);
dest.writeInt(alacartePackId);
dest.writeDouble(price);
}
但构造函数应该是:
private AlacartePack(Parcel in){
this.alacartePackName = in.readString();
this.alacartePackId = in.readInt();
this.price = in.readDouble();
}
这同样适用于您的AlacarteCategory
课程。
除此之外,您还应该修改AlacarteCategory
中阅读列表的方式。像这样:
private AlacarteCategory(Parcel in){
this.categoryName = in.readString();
this.categoryId = in.readInt();
this.alacartePackList = new ArrayList<AlacartePack>();
in.readList(this.alacartePackList, AlacarteCategory.class.getClassLoader());
}
然后将其列为如下列表:
@Override
public void writeToParcel(Parcel dest, int i) {
dest.writeString(categoryName);
dest.writeInt(categoryId);
dest.writeList(alacartePackList);
}