AndroidRuntime error: Parcel: unable to marshal value

JavaAndroid

Java Problem Overview


I am trying to pass a HashMap to a new activity using the intent.puExtra function. Stepping through the debugger it seems that it adds the HashMap no problem, however when startActivty() is called I get a runtime error stating that Parcel: unable to marshal value com.appName.Liquor.

Liquor is a custom class that I created, and I believe it, in combination with a HashMap, is causing the problem. If I pass a string rather than my HashMap it loads the next activity no problem.

Main Activity

lv.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {

      String cat = ((TextView) view).getText().toString();
      Intent i = new Intent(OhioLiquor.this, Category.class);
      i.putExtra("com.appName.cat", _liquorBase.GetMap());
      startActivity(i);

Liquor Class

public class Liquor
{
public String name;
public int code;
public String category;

private HashMap<String, Bottle> _bottles;

public Liquor()
{
	_bottles = new HashMap<String, Bottle>();
}

public void AddBottle(Bottle aBottle)
{
	_bottles.put(aBottle.size, aBottle);
}
}

Sub Activity

public void onCreate(Bundle savedInstanceState)
{
	super.onCreate(savedInstanceState);
	
	HashMap<Integer, Liquor> map = (HashMap<Integer, Liquor>)getIntent().getSerializableExtra("com.appName.cat");
	        
	setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, GetNames(map)));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

When the runtime error exists it never makes it into the sub activity class. So I'm pretty sure the problem exists in the adding of the HashMap to the intent, and based off the error I believe my Liquor class is the cause, but I can't figure out why.

Your help will be much appreciated. Thanks!

Java Solutions


Solution 1 - Java

Your HashMap itself is serializable but is the Bottle class serializable? If not, it will not serialize and will throw errors at runtime. Make the Bottle class implement the java.io.Serializable interface

Solution 2 - Java

I was getting this error because my class wasnt Serializable. To make my class serializable I had to add : Serializable at the end of class to make is Serializable. Just like this

class Shirt(val price: Double, val discountedPrice: Double) : Serializable

Make sure to import Serializable like this

import java.io.Serializable

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJaredView Question on Stackoverflow
Solution 1 - JavanaikusView Answer on Stackoverflow
Solution 2 - JavaZohab AliView Answer on Stackoverflow