Parcelable 中的解组困难

发布于 2024-12-05 05:10:18 字数 4445 浏览 1 评论 0原文

这是我写入包裹的代码,

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

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

但是当我添加关于吉祥物的这 3 行时,我的应用程序崩溃了。我对此感到非常困惑,我不明白为什么会发生这种情况,有人可以让我知道这个问题吗?

我收到运行时异常,并出现问题,说 Parcelable 存在解组错误

这是我收到的异常

 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-12-12 05:10:18

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

在读取/写入吉祥物信息时,您可以使用布尔值,而不是检查它们是否为空:

    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;
    }

并且您还可以读取更多值比你写给包裹的还要多。值需要以相同的顺序写入和读取。

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.

雨落□心尘 2024-12-12 05:10:18

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

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

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.

和我恋爱吧 2024-12-12 05:10:18

我以某种方式解决了这个问题。我将其设置为静态,而不是使其可分割。这可能不是使其静态的标准方法。但我感觉包裹的大小是有限制的。如果我添加一些 15 个或更多变量,它就不起作用。所以它现在对我有用。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文