1

Here is my code of writing to a parcel

public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(poiDescription);
        dest.writeString(latitude);
        dest.writeString(longitude);
        dest.writeString(placeName);
        dest.writeString(plistPath);
        dest.writeString(poiDirName);
            if (null != isMascotPresent)
                dest.writeString(isMascotPresent);
            if (null != startMascottTime)
                dest.writeInt(startMascottTime);
            if (null != mascottDuration)
                dest.writeInt(mascottDuration);
}

public PointOfInterest(Parcel source) { 
        poiDescription = source.readString();
        latitude = source.readString();
        longitude = source.readString();
        placeName = source.readString();
        plistPath = source.readString();
        poiDirName = source.readString();
        audioLinkDownload = source.readString();
        audioLinkStream = source.readString();
        poiName = source.readString();
        poiIndex = source.readInt();
        poiPaused = source.readString();
        source.readList(imageList, null);
        source.readList(durationList, null);
        if (null != isMascotPresent)
            isMascotPresent = source.readString();
        if (null != startMascottTime)
               startMascottTime=source.readInt();
        if (null != mascottDuration)
                  mascottDuration=source.readInt();
}


This is how I am reading the values
listOfPOI = getIntent().getParcelableArrayListExtra("poi");

This codw works fine without the mascot above.

But when I add those 3 lines about mascot, my application is crashing. I am breaking my head onthis, I am not getting why is this happening, can anyone please let me know the issue?

I am getting the run time exception and problem saying there is unmarshalling error for the parcelable

This is the exception I am getting

 ERROR/AndroidRuntime(2061): FATAL EXCEPTION: main
 ERROR/AndroidRuntime(2061): java.lang.RuntimeException: Unable to start activity       ComponentInfo{com.Invenger/com.Invenger.Player.Audio}: java.lang.RuntimeException: Parcel android.os.Parcel@40570848: Unmarshalling unknown type code 7536748 at offset 1064
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 ERROR/AndroidRuntime(2061):     at android.os.Handler.dispatchMessage(Handler.java:99)
 ERROR/AndroidRuntime(2061):     at android.os.Looper.loop(Looper.java:130)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 ERROR/AndroidRuntime(2061):     at java.lang.reflect.Method.invokeNative(Native Method)
 ERROR/AndroidRuntime(2061):     at java.lang.reflect.Method.invoke(Method.java:507)
 ERROR/AndroidRuntime(2061):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ERROR/AndroidRuntime(2061):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 ERROR/AndroidRuntime(2061):     at dalvik.system.NativeStart.main(Native Method)
 ERROR/AndroidRuntime(2061): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@40570848: Unmarshalling unknown type code 7536748 at offset 1064
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readValue(Parcel.java:1913)
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readListInternal(Parcel.java:2092)
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readArrayList(Parcel.java:1536)
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readValue(Parcel.java:1867)
ERROR/AndroidRuntime(2061):     at android.os.Parcel.readMapInternal(Parcel.java:2083)
ERROR/AndroidRuntime(2061):     at android.os.Bundle.unparcel(Bundle.java:208)
ERROR/AndroidRuntime(2061):     at   android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
 ERROR/AndroidRuntime(2061):     at android.content.Intent.getParcelableArrayListExtra(Intent.java:3448)
    ERROR/AndroidRuntime(2061):     at com.Invenger.Player.Audio.onCreate(Audio.java:162)
    ERROR/AndroidRuntime(2061):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
   ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
   ERROR/AndroidRuntime(2061):     ... 11 more
2
  • What is the type of isMascotPresent?
    – i_am_jorf
    Sep 17, 2011 at 5:44
  • isMascotPresent is of type String
    – tejas
    Sep 17, 2011 at 5:45

3 Answers 3

12

Unless you're initializing isMascotPresent, startMascottTime and mascottDuration somewhere they're always going to be null in the constructor, PointOfInterest(Parcel source). So even if you wrote values when parceling the object they wouldn't be read and the following values would be incorrect.

Instead of checking if they're null when reading/writing the mascot info you could use a boolean:

    if (null != isMascotPresent) {
        dest.writeByte((byte) 1);
        dest.writeString(isMascotPresent);
        dest.writeInt(startMascottTime);
        dest.writeInt(mascottDuration);
    } else {
        dest.writeByte((byte) 0);
    }

And to read it back in:

    if(source.readByte() == 1) {
        isMascotPresent = source.readString();
        startMascottTime = source.readInt();
        mascottDuration = source.readInt();
    } else {
        isMascotPresent = null;
        startMascottTime = null;
        mascottDuration = null;
    }

And you're also reading more values in than you're writing out to the parcel. Values need to be written and read in the same order.

3
  • I am setting the values for these in other place, so do I need to initialize them to null or empty character? I think no, right? Anyways, I will try this soon and will let you know the result. Thanks
    – tejas
    Sep 23, 2011 at 8:35
  • The only place where you can set the values before the constructor is at the declaration: 'String isMascotPresent;' and 'String isMascotPresent = null;' means it'll always be null in the constructor. 'String isMascotPresent = "something";' means it'll always be non-null in the constructor.
    – Johnbot
    Sep 26, 2011 at 9:28
  • Thanks for the suggestion! I was actually doing the null-check and catching exception, but this is a MUCH better idea! Better to not throw the exceptions at all an use a simple byte to store whether or not optional values are present.
    – Tom
    Jan 3, 2013 at 20:09
0

You can use android studio plugin, which generates Android Parcelable boilerplate code for you. File->Settings->Plugins->Browse Repositories->search for Android Parcelable code generator. OR https://plugins.jetbrains.com/plugin/7332?pr=

Problem is in sequence of writing and reading of object...sequence should be same, as it is like reading file.

-3

I fixed the problem somehow. I made it as static instead of making it parcelable.Ihis may not be the standard way, of making it static. But I feel that there is a limit on the size of parcel. If I add some 15 or more variable it is not working. So it is working for me now.

Any better answer will be accepted

4
  • 3
    Parcels by themselves do not have limits, and they are very efficient. The issue of size arises when transferring them across processes with AIDL. 2MB is a hard limit on any IPC transaction. Not working with 15+ variables might just be a matter of optional variables not taken care of properly, or placing the read/write calls in the wrong order.
    – Tom
    Jan 3, 2013 at 20:20
  • Try killing your process while activity is in the background and then coming back to it. Statics is not always an option. Entire idea of parcelables is ability to persist the object across the processes (or persist it while your process is dead). Aug 27, 2013 at 22:50
  • @PaulTurchenko, first of all thank you for answering. But there was no need for you to downvote my answer. I had clearly mentioned any better answer will be accepted. I had written what I had done. When ever you down vote, please see the effort also and think before you ink!
    – tejas
    Aug 28, 2013 at 7:03
  • I'm also down-voting because anyone searching this problem should not do this to avoid it. @Johnbot's answer above is correct. Mar 10, 2014 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.