Reputation: 1760
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!
Upvotes: 139
Views: 120159
Reputation: 9574
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
Upvotes: 7
Reputation: 24472
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
Upvotes: 321