在parcelable中解组很困难

时间:2011-09-17 05:09:33

标签: android unmarshalling parcelable runtimeexception

这是我写入包裹的代码

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");

如果没有上面的吉祥物,这个鳕鱼就可以正常工作。

但是当我添加关于吉祥物的3行时,我的应用程序崩溃了。我正在打破这个问题,我不知道为什么会发生这种情况,有人可以让我知道这个问题吗?

我收到运行时异常和问题,说明可以分配

的解组错误

这是我得到的例外

 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

3 个答案:

答案 0 :(得分:12)

除非您在某个地方初始化isMascotPresentstartMascottTimemascottDuration,否则他们在构造函数中始终为null PointOfInterest(Parcel source)。因此,即使您在分配对象时写入值,也不会读取它们,并且以下值将是不正确的。

在阅读/撰写吉祥物信息时,您可以使用null而不是检查它们是否boolean

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

再读回来:

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

而且你也在阅读更多的价值而不是写到包裹里。需要以相同的顺序写入和读取值。

答案 1 :(得分:0)

您可以使用android studio插件,为您生成Android Parcelable样板代码。 文件 - >设置 - >插件 - >浏览存储库 - >搜索Android Parcelable代码生成器。 要么 https://plugins.jetbrains.com/plugin/7332?pr=

问题是按顺序写入和读取对象...序列应该是相同的,就像读取文件一样。

答案 2 :(得分:-3)

我以某种方式解决了问题。我把它作为静态而不是让它变得可以分解。这可能不是标准的方法,使它变得静止。但我觉得包裹的大小有限制。如果我添加15个或更多变量,它就无法正常工作。所以它现在对我有用。

任何更好的答案都将被接受

相关问题