1

I'm developing an android application in which fragments use dynamically created views. Part of my onSaveInstanceState() method in my fragment is as follows:

outState.putSerializable("intToRow", (HashMap<Integer, View>) intToRow);

When I comment out this line everything works fine. I've left this line in for now and here's the problem: When I rotate the screen, this works fine and I can access it in onCreate() using

intToRow = (HashMap) savedInstanceState.getSerializable("intToRow");

And when the back button on the action bar is pressed the fragment and activity close without any problems. So why is it that when I press the Home button or the multitasking button, the app crashes? The stack trace is:

java.lang.RuntimeException: Parcel: unable to marshal value android.widget.TextView{383ee509 V.ED.... ........ 246,0-492,57 #3}
        at android.os.Parcel.writeValue(Parcel.java:1337)
        at android.os.Parcel.writeMapInternal(Parcel.java:614)
        at android.os.Parcel.writeMap(Parcel.java:598)
        at android.os.Parcel.writeValue(Parcel.java:1255)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
        at android.os.Bundle.writeToParcel(Bundle.java:1096)
        at android.os.Parcel.writeBundle(Parcel.java:663)
        at android.app.FragmentState.writeToParcel(Fragment.java:147)
        at android.os.Parcel.writeTypedArray(Parcel.java:1191)
        at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:380)
        at android.os.Parcel.writeParcelable(Parcel.java:1357)
        at android.os.Parcel.writeValue(Parcel.java:1262)
        at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
        at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
        at android.os.Bundle.writeToParcel(Bundle.java:1096)
        at android.os.Parcel.writeBundle(Parcel.java:663)
        at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3116)
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3695)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5832)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

I've seen many questions about the whole Parcel: unable to marshal value error but mine doesn't seem to match any of them. In my app, an orientation change and destroying the fragment seem to work perfectly but pressing the Home button makes it crash. I've been through the Android lifecycle but I can't see anything that should happen when Home is pressed that wouldn't happen otherwise.

Sreehari
  • 5,621
  • 2
  • 25
  • 59
DrRobot
  • 29
  • 6
  • the fragment and activity are too big to post everything – DrRobot Feb 01 '16 at 11:10
  • problem is your TextView not initialize ..check that your code – sasikumar Feb 01 '16 at 11:14
  • I'll check if that's the reason. But if it is, then why do orientation changes work fine? – DrRobot Feb 01 '16 at 11:16
  • 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.......http://stackoverflow.com/questions/3818745/androidruntime-error-parcel-unable-to-marshal-value – sasikumar Feb 01 '16 at 11:18
  • refer this question http://stackoverflow.com/questions/7894353/android-having-difficulty-passing-an-object-from-one-activity-to-another-using – ELITE Feb 01 '16 at 11:20
  • sasikumar, The TextViews are all being initialised for sure. I'll try implementing serializable but once again, if this is the reason, why does orientation change work fine? ELITE, I don't understand, aren't I already using bundles? – DrRobot Feb 01 '16 at 11:33
  • @DrRobot i have the same problem did you manage to solve it? – has19 Aug 02 '16 at 18:07
  • @has19 Yeah, everywhere I was using View, I changed it to MyView, which was just a class which extended View and implemented Serializable. I didn't work out why it was working when the activity was closed with the back button even though onSaveInstanceState() was definitely called. – DrRobot Aug 05 '16 at 15:49

2 Answers2

1

This answer did get me in the right direction but the implementation was confusing. The class could be

public class item { ... }

To resolve the marshal problem simply make the class serializable

public class item implements Serializable { .. } 

Hope this helps someone out...

0

I was struggling to find a solution to this problem too.

My square and circle buttons caused the app to crash, while changing the orientation didn't cause it to crash.

I thought it must have been something to do with RecyclerView or Fragment, but I finally realized it was a Parcel problem.

There don't seem to be any clear solutions to the problem (I searched and troubleshooted for over an hour), so here is the solution:

The guy who asked the question showed his logcat which says

Parcel: unable to marshal value ______

For him, the ______ is android.widget.TextView, but it will likely be something else for other people.

Whatever that class is, the problem is that it is being Parceled but is not implementing Parcelable.

So the solution is to implement Parcelable for that class.

I was confused because I had implemented Parcelable for the class I was Parceling, but I didn't know I also had to implement Parcelable for all the subclasses that would be Parceled with that main object class.

This is how to implement Parcelable for the relevant class:

1) Check logcat and find where it says "unable to marshal value ______" (where ______ will be the relevant class for your problem.)

2) Go to the relevant class's .java file (if the issue is a default class or not your custom class, you will need to make a new class that extends that class, and then go to that class's .java file)

3) Left-click on the class name in the editor, and press alt+insert (to show the Generate menu), then click on Parcelable, then press OK

4) Try to run your app again and reproduce the problem. If it happens again, check logcat again because you may have more than 1 class which need to implement Parcelable (I had 2 subclasses which needed to implement Parcelables), so check logcat each time and do the same thing for each class.

5) If you have multiple activities/fragments, try reproducing the problem on each page, so that you don't miss any other classes with the same problem.

That's it!

Cheers homies.