Parcelable 中的解组困难
这是我写入包裹的代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非您在某个地方初始化
isMascotPresent
、startMascottTime
和mascottDuration
,否则它们在构造函数中始终为null
,PointOfInterest(地块来源)
。因此,即使您在打包对象时写入了值,它们也不会被读取,并且以下值将是不正确的。在读取/写入吉祥物信息时,您可以使用布尔值,而不是检查它们是否为空:
并将其读回:
并且您还可以读取更多值比你写给包裹的还要多。值需要以相同的顺序写入和读取。
Unless you're initializing
isMascotPresent
,startMascottTime
andmascottDuration
somewhere they're always going to benull
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 aboolean
:And to read it back in:
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.
您可以使用 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.
我以某种方式解决了这个问题。我将其设置为静态,而不是使其可分割。这可能不是使其静态的标准方法。但我感觉包裹的大小是有限制的。如果我添加一些 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