Android 在包裹中拆封的困难
这是我写包裹的密码Android 在包裹中拆封的困难,android,unmarshalling,parcelable,runtimeexception,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);
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很好用
但当我添加这三行关于吉祥物的内容时,我的应用程序崩溃了。我很生气,我不明白为什么会这样,有人能告诉我这个问题吗
我遇到运行时异常,问题是包裹有解组错误
这是我得到的例外
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
我设法解决了这个问题。我把它做成了静态的,而不是可以打包的。这可能不是使它成为静态的标准方法。但是我觉得包裹的大小是有限制的。如果我添加15个或更多的变量,它就不起作用了。所以它现在对我起作用了
任何更好的答案都会被接受我不知怎么解决了这个问题。我把它做成了静态的,而不是可以打包的。这可能不是使它成为静态的标准方法。但是我觉得包裹的大小是有限制的。如果我添加15个或更多的变量,它就不起作用了。所以它现在对我起作用了
任何更好的答案都将被接受除非您正在初始化
isMascotPresent
、startmascottime
和mascottDuration
构造函数中的null
,兴趣点(包源)
。因此,即使在打包对象时写入值,也不会读取这些值,并且以下值将不正确
在读取/写入吉祥物信息时,不必检查它们是否为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;
}
而且,在中读取的值也比在包裹中写入的值要多。值需要以相同的顺序写入和读取。除非您正在初始化
ismascottpresent
,startmascottime
和mascottDuration
构造函数中的null
某个地方,兴趣点(包源)
。因此,即使在打包对象时写入值,也不会读取这些值,并且以下值将不正确
在读取/写入吉祥物信息时,不必检查它们是否为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;
}
而且,在中读取的值也比在包裹中写入的值要多。值需要以相同的顺序写入和读取。您可以使用android studio插件,它为您生成android可打包的样板代码。 文件->设置->插件->浏览存储库->搜索Android Parcelable代码生成器。 或
问题在于对象的写入和读取顺序……顺序应该是相同的,就像读取文件一样 您可以使用android studio插件,它可以为您生成android Parcelable样板代码。 文件->设置->插件->浏览存储库->搜索Android Parcelable代码生成器。 或
问题在于对象的写入和读取顺序……顺序应该是相同的,就像读取文件一样
isMascotPresent
的类型是什么?isMascotPresent的类型是stringisMascotPresent
的类型是什么?isMascotPresent的类型是string我正在其他地方设置这些值,所以我需要将它们初始化为null或空字符吗?我想没有,对吧?不管怎样,我会很快尝试,并会让你知道结果。感谢您唯一可以在构造函数之前设置值的地方是声明:“String isMascotPresent;”和'String isMascotPresent=null;'意味着它在构造函数中始终为空。”字符串isMascotPresent=“某物”;'意味着它在构造函数中总是非空的。谢谢你的建议!我实际上是在做空检查和捕获异常,但这是一个更好的主意!最好不要抛出异常,而是使用一个简单的字节来存储是否存在可选值。我正在其他位置设置这些值,所以是否需要将它们初始化为null或空字符?我想没有,对吧?不管怎样,我会很快尝试,并会让你知道结果。感谢您唯一可以在构造函数之前设置值的地方是声明:“String isMascotPresent;”和'String isMascotPresent=null;'意味着它在构造函数中始终为空。”字符串isMascotPresent=“某物”;'意味着它在构造函数中总是非空的。谢谢你的建议!我实际上是在做空检查和捕获异常,但这是一个更好的主意!最好不要抛出异常,而是使用一个简单的字节来存储是否存在可选值。包裹本身没有限制,而且非常有效。在使用AIDL跨流程传输时,会出现大小问题。2MB是任何IPC事务的硬限制。不使用15个以上的变量可能只是可选变量没有得到正确处理,或者读/写调用顺序错误。请尝试在活动处于后台时终止进程,然后返回。静态并不总是一个选项。包裹的整个概念是能够在整个进程中持久化对象(或者在进程停止时持久化对象)。@PaulTurchenko,首先感谢您的回答。但你没有必要否决我的回答。我已经清楚地提到,任何更好的答案都会被接受。我已经把我所做的写下来了。当你否决投票时,请看到你的努力,并在你投票前思考!我也投了反对票,因为任何搜索这个问题的人都不应该这样做来避免它@约翰伯特上面的回答是正确的。包裹本身没有限制,而且效率很高。在使用AIDL跨流程传输时,会出现大小问题。2MB是任何IPC事务的硬限制。不使用15个以上的变量可能只是一个可选变量的问题,没有得到适当的处理