2011-09-17 73 views
1

这是我写一个包裹困难解组在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 [email protected]: 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 [email protected]: 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 
+0

“isMascotPresent”的类型是什么? –

+0

isMascotPresent是字符串类型 – tejas

回答

-3

我莫名其妙地解决了这一问题除外。我把它做成静态的,而不是让它变成可以分类的。这可能不是标准的方法,也不是静态的。但我觉得包裹的大小有限制。如果我添加了15个或更多的变量,它不起作用。所以现在它为我工作。

没有更好的答案除非你初始化isMascotPresentstartMascottTimemascottDuration地方他们总是会null在构造函数中,PointOfInterest(Parcel source)接受

+3

包裹本身没有限制,而且它们非常有效。在使用AIDL跨进程传输它们时,会出现大小问题。 2MB是任何IPC交易的硬性限制。不能处理15个以上的变量可能只是可选变量没有正确处理的问题,或者以错误的顺序放置读/写调用。 – Tom

+0

尝试在活动处于后台并尝试返回时尝试查杀您的进程。静力学并不总是一种选择。整个可分解的想法是将对象持久存储在进程中的能力(或者在进程停止时持久化)。 –

+0

@PaulTurchenko,首先感谢您的回答。但是你没有必要降低我的答案。我已经明确提到任何更好的答案都会被接受。我写了我所做的。当你投票的时候,请看看你的努力,并在墨水之前思考! – tejas

12

。因此,即使您在分区对象时编写了值,也不会读取它们,并且以下值不正确。

而不是检查,如果读取/写入吉祥物的信息,您可以使用时,他们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; 
    } 

而你同时阅读更多的价值,而不是写给包裹。值需要按照相同的顺序写入和读取。

+0

我在其他地方设置这些值,所以我需要将它们初始化为空或空字符?我想不,对不对?无论如何,我会尽快尝试,并会让你知道结果。谢谢 – tejas

+0

在构造函数处于声明之前,您可以设置值的唯一位置:'String isMascotPresent;'和'String isMascotPresent = null;'意味着它在构造函数中始终为空。 'String isMascotPresent =“something”;'意味着它在构造函数中始终是非空的。 – Johnbot

+0

感谢您的建议!我实际上正在做空检查和捕捉异常,但这是一个更好的主意!最好不要抛出异常,只需使用一个简单字节来存储是否存在可选值。 – Tom

0

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

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