Geshner Inagaki
Geshner Inagaki

Reputation: 87

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?

Upvotes: 3

Views: 2859

Answers (2)

Isa Vell
Isa Vell

Reputation: 89

I had same error and the lines causing the error were these:

 ingredients = in.readParcelable(Ingredient.class.getClassLoader());

and

public static final Parcelable.Creator<RecipeContent> CREATOR = new Parcelable.Creator<RecipeContent>() {
    @Override
    public RecipeContent createFromParcel(Parcel in) {
        return new RecipeContent(in);
    }

So, what I did and it helped was to arrange the in.read items in the same way as the values in the Parcel are arranged.

Here are the details:

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

And then using the same order for the 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();
}

Hope this helps for you too.

Upvotes: 1

Geshner Inagaki
Geshner Inagaki

Reputation: 87

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

Upvotes: 1

Related Questions