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

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或“等级” ID(“等级”是指产品具有颜色和尺寸的情况),但永远不会同时具有两个值。

所以我这样实现:

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

关于android - 拆封可包裹 list 时出现ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35589096/

10-10 00:20