Home » Android » java – AndroidRuntime error: Parcel: unable to marshal value

java – AndroidRuntime error: Parcel: unable to marshal value

Posted by: admin March 11, 2020 Leave a comment

Questions:

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!

How to&Answers:

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