-1

I tried to pass an HashMap via intent from fragment to activity, but without success. this is my code:

    private HashMap<Integer, JSONObject> myData;

    Intent intent = new Intent(this.getActivity(),MyActivity.class);
    intent.putExtra("myMap", myData);
    startActivity(intent);

and I got a runtimeExeption:

    java.lang.RuntimeException: Parcel: unable to marshal value

I would be happy if someone can help me with that! I need a way to resolve it, I know that JSONObject is not Serializable. The value is not a custom object, therfore I can't implement Serializable there. Thank a lot!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
yoc
  • 214
  • 2
  • 13
  • Possible duplicate of [AndroidRuntime error: Parcel: unable to marshal value](https://stackoverflow.com/questions/3818745/androidruntime-error-parcel-unable-to-marshal-value) – Selvin Jul 24 '18 at 13:33
  • this is from stacktrace - `java.lang.RuntimeException: Parcel: unable to marshal value` – yoc Jul 24 '18 at 13:35

1 Answers1

0

Instead of

HashMap<Integer, JSONObject> myData

you should use

HashMap<Integer, String> myData

And add the items as Strings instead of JSONObject:

myData.put(0, myJsonObject.toString());

then, on your second activity, you create the JSON when needed:

new JSONObject(myData.get(0));

The reason:

JSONObject is not serializable by default but String is (notice it implements Serializable).

Tiago Ornelas
  • 1,109
  • 8
  • 21