解组 parcelable 列表时出现 ClassNotFoundException

ClassNotFoundException when unmarshalling list of parcelable

我正在尝试在两个活动之间发送一个对象。 订单对象包含项目列表 我是这样实现的:

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

在 PriceTableItem 中,我有一种情况,它可以包含产品 ID 或 "grade" ID("grade" 是指产品具有颜色和尺寸)但永远不会同时具有这两个值。

所以我是这样实现的:

@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;
}

当我将订单对象从 OrderListActivity 传递到 OrderDetailActivity 时出现问题。它读取了我的项目列表之前的所有属性。当它尝试读取 OrderItem 上的 PriceTable 时,我得到:

java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.intelecto.intesigmobile/br.com.intelecto.intesigmobile.activity.PedidoDetailActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling:

问题行是:

priceTableItem =  in.readParcelable(PriceTableItem.class.getClassLoader());

关于如何解决这个问题有什么想法吗?

仍然不知道是什么导致了错误,但我解决了下面的问题

当我传递订单值时,我仅使用 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");

我有同样的错误,导致错误的行是这些:

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

所以,我所做的是按照与 Parcel 中的值相同的方式排列 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();
}

希望这对你也有帮助。