Search code examples
androidparcelableparcel

ClassNotFoundException when unmarshalling list of parcelable


I'm trying to send an object between two activities. The order Object contains a list of item witch I implemented like this:

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

At the PriceTableItem I have a situation where it can contais a product id or a "grade" id ("grade" is when the product have color and size) but never have both values.

so I implemented like this:

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

The problem occur when I pass the order object from my OrderListActivity to OrderDetailActivity. It read all attributes before my list of item. When it try to read the PriceTable on OrderItem I get:

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

The problem line is:

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

Any ideas on how to solve this?


Solution

  • Still don't know what caused the error, but I solved the problem like bellow

    When I was passing the order value I was using only the Intent to do it, like this:

    Intent i = new Intent(this, OrderDetailActivity.class);
    i.putExtras("order", order);
    startActivity(i);
    

    And, to read it I was doing like this:

    Order order = getIntent().getParcelableExtra("order");
    

    So, I used a Bundle for pass the value.

    Intent i = new Intent(this, OrderDetailActivity.class);
    
    Bundle b = new Bundle();
    b.putParcelable("order", order);
    
    i.putExtras(b);
    startActivity(i);
    

    And

    Bundle b = getIntent().getExtras();
    
    Order order = b.getParcelable("order");