我试图在两个活动之间发送一个对象。 订单对象包含我实施的项目列表:
>
在PriceTableItem我有一种情况,它可以包含产品ID或等级" id("等级"是产品有颜色和尺寸的时候),但从不同时拥有这两个值。
所以我这样实现了:
OrderItem Object:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeParcelable(priceTableItem, flags);
dest.writeInt(qunatity);
dest.writeDouble(value); // quantity * unitValue
dest.writeDouble(discount);
}
protected OrderItem(Parcel in) {
id = in.readInt();
priceTableItem = in.readParcelable(PriceTableItem.class.getClassLoader());
quantity = in.readInt();
value = in.readDouble();
discount = in.readDouble();
}
当我将订单对象从OrderListActivity传递到OrderDetailActivity时,会出现问题。它在我的项目列表之前读取所有属性。当它尝试读取OrderItem上的PriceTable时,我得到:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeParcelable(priceTable, flags);
dest.writeValue(produto);
dest.writeValue(grade);
dest.writeDouble(unitPrice);
dest.writeByte((byte) (isActive ? 1 : 0));
}
protected PriceTableItem(Parcel in) {
id = in.readInt();
priceTable = in.readParcelable(PriceTable.class.getClassLoader());
product = (Product) in.readValue(Product.class.getClassLoader());
grade = (Grade) in.readValue(Grade.class.getClassLoader());
unitPrice = in.readDouble();
isactive = in.readByte() != 0;
}
问题在于:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.intelecto.intesigmobile/br.com.intelecto.intesigmobile.activity.PedidoDetailActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
关于如何解决这个问题的任何想法?
答案 0 :(得分:1)
我有同样的错误,导致错误的行是这些:
ingredients = in.readParcelable(Ingredient.class.getClassLoader());
和
public static final Parcelable.Creator<RecipeContent> CREATOR = new Parcelable.Creator<RecipeContent>() {
@Override
public RecipeContent createFromParcel(Parcel in) {
return new RecipeContent(in);
}
所以,我所做的和它帮助的是以与包裹中的值相同的方式排列in.read项目。
以下是详细信息:
public RecipeContent(int id, String recipeName, List<Ingredient> ingredients,
List<BakingStep> bakingSteps, String recipeImage) {
this.id = id;
this.recipeName = recipeName;
this.ingredients = ingredients;
this.bakingSteps = bakingSteps;
this.recipeImage = recipeImage;
}
然后对in.read使用相同的顺序:
public RecipeContent(Parcel in) {
id = in.readInt();
recipeName = in.readString();
ingredients = in.readParcelable(Ingredient.class.getClassLoader());
bakingSteps = in.readParcelable(BakingStep.class.getClassLoader());
recipeImage = in.readString();
}
希望这对你也有帮助。
答案 1 :(得分:0)
仍然不知道导致错误的原因,但我解决了问题,如下文
当我传递订单值时,我只使用Intent来执行此操作,如下所示:
Intent i = new Intent(this, OrderDetailActivity.class);
i.putExtras("order", order);
startActivity(i);
并且,为了阅读它,我这样做:
Order order = getIntent().getParcelableExtra("order");
所以,我使用Bundle来传递值。
Intent i = new Intent(this, OrderDetailActivity.class);
Bundle b = new Bundle();
b.putParcelable("order", order);
i.putExtras(b);
startActivity(i);
和
Bundle b = getIntent().getExtras();
Order order = b.getParcelable("order");