嵌套解析:RuntimeException - 在偏移量 440 处解组未知类型代码 3211319

发布于 2024-10-02 07:31:47 字数 4087 浏览 1 评论 0原文

我需要向 Activity 发送一些数据,该活动可能在不同的上下文中运行。 为此,我创建了一个类 A,它有一个数据类型为 B 的 ArrayList 作为其实例成员之一。我将B类声明为A类的内部类。为了通过 Intent 发送 A 类的实例,我将 A 类和 B 类都设置为 Parcelable

类结构是这样的(这不包括完整的代码,例如为使类Parcelable而编写的代码):

public class A implements Parcelable{

   public class B implements Parcelable{
         public ArrayList<String> value;
         ....
         .... 
          public void writeToParcel(Parcel dest, int flags) {
            dest.writeList(value);
           }
        ....
        ....
   }

   public List<B> group;
   public String name;

   ....
   .... 
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(group);
        dest.writeString(name);
       }
   ....
   ....
}

我使用了putExtra(String name,Parcelable value)函数放置数据。

但在接收端,我得到了以下异常:

 Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1087): 1289817569622 java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.SET_VOIP_SUPP_SERVICE_REQUEST_LOCAL (has extras) } in com.hsc.example.android.MyApp.MyAppActuvity$1@43b409b0
E/AndroidRuntime( 1087):        at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:765)
E/AndroidRuntime( 1087):        at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1087):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1087):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1087):        at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 1087):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1087):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1087):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 1087):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 1087):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1087): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@43b51240: Unmarshalling unknown type code 3211319 at offset 440
E/AndroidRuntime( 1087):        at android.os.Parcel.readValue(Parcel.java:1777)
E/AndroidRuntime( 1087):        at android.os.Parcel.readListInternal(Parcel.java:1956)
E/AndroidRuntime( 1087):        at android.os.Parcel.readList(Parcel.java:1302)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A.<init>(A.java:61)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A.<init>(A.java:57)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A$1.createFromParcel(A.java:67)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A$1.createFromParcel(A.java:1)
E/AndroidRuntime( 1087):        at android.os.Parcel.readParcelable(Parcel.java:1845)
E/AndroidRuntime( 1087):        at android.os.Parcel.readValue(Parcel.java:1713)
E/AndroidRuntime( 1087):        at android.os.Parcel.readMapInternal(Parcel.java:1947)
E/AndroidRuntime( 1087):        at android.os.Bundle.unparcel(Bundle.java:169)
E/AndroidRuntime( 1087):        at android.os.Bundle.getParcelable(Bundle.java:1037)
E/AndroidRuntime( 1087):        at android.content.Intent.getParcelableExtra(Intent.java:3269)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.MyAppActuvity$1.onReceive(MyAppActuvity.java:219)
E/AndroidRuntime( 1087):        at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:754)
E/AndroidRuntime( 1087):        ... 9 more

然后我将类 B 移到了类 A 之外(因为我认为这可能是内部类的问题,并声明了 CREATOR 静态。当类 B 时,此静态声明不存在是 A 类的内部类)。但这并没有帮助。

看来这个问题是由于嵌套Parceling造成的。

有什么建议吗?

注意:当我使用 Bundle 类的 android.os.Bundle.putParcelable(String key, Parcelable value) 函数,然后使用 android.os 对其进行解组时。 Bundle.getParcelable(String key) 然后一切都很好。所以看来问题仅与意图有关。

I need to send some data to an Activity, which may be running in different context.
For this I created a class say A, which has an ArrayList of datatype say B as its one of instance member . I declared class B as Class A's Inner class. To send this class A's instance through Intent, I made class A and B both Parcelable.

Class structure is something like this (this does not include the complete code e.g. code written to make the classes Parcelable):

public class A implements Parcelable{

   public class B implements Parcelable{
         public ArrayList<String> value;
         ....
         .... 
          public void writeToParcel(Parcel dest, int flags) {
            dest.writeList(value);
           }
        ....
        ....
   }

   public List<B> group;
   public String name;

   ....
   .... 
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(group);
        dest.writeString(name);
       }
   ....
   ....
}

I used the putExtra (String name, Parcelable value) function to put data.

But on the receiving side, I got the following exception:

 Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 1087): 1289817569622 java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.SET_VOIP_SUPP_SERVICE_REQUEST_LOCAL (has extras) } in com.hsc.example.android.MyApp.MyAppActuvity$1@43b409b0
E/AndroidRuntime( 1087):        at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:765)
E/AndroidRuntime( 1087):        at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1087):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1087):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1087):        at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 1087):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1087):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1087):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 1087):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 1087):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1087): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@43b51240: Unmarshalling unknown type code 3211319 at offset 440
E/AndroidRuntime( 1087):        at android.os.Parcel.readValue(Parcel.java:1777)
E/AndroidRuntime( 1087):        at android.os.Parcel.readListInternal(Parcel.java:1956)
E/AndroidRuntime( 1087):        at android.os.Parcel.readList(Parcel.java:1302)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A.<init>(A.java:61)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A.<init>(A.java:57)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A$1.createFromParcel(A.java:67)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.A$1.createFromParcel(A.java:1)
E/AndroidRuntime( 1087):        at android.os.Parcel.readParcelable(Parcel.java:1845)
E/AndroidRuntime( 1087):        at android.os.Parcel.readValue(Parcel.java:1713)
E/AndroidRuntime( 1087):        at android.os.Parcel.readMapInternal(Parcel.java:1947)
E/AndroidRuntime( 1087):        at android.os.Bundle.unparcel(Bundle.java:169)
E/AndroidRuntime( 1087):        at android.os.Bundle.getParcelable(Bundle.java:1037)
E/AndroidRuntime( 1087):        at android.content.Intent.getParcelableExtra(Intent.java:3269)
E/AndroidRuntime( 1087):        at com.hsc.example.android.MyApp.MyAppActuvity$1.onReceive(MyAppActuvity.java:219)
E/AndroidRuntime( 1087):        at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:754)
E/AndroidRuntime( 1087):        ... 9 more

Then I moved the class B, outside the class A (As I thought this may be problem of inner class, and declared the CREATOR static. This static declaration was not present when class B was an inner class of Class A). But it did not helped.

This seems that this problem is due to nested Parceling.

Any suggestion ??

NOTE: when I used android.os.Bundle.putParcelable(String key, Parcelable value) function of Bundle class, and then unmarshalled it using android.os.Bundle.getParcelable(String key) then everything is fine. So it seems that the problem is with respect to Intents only.

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

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

发布评论

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

评论(2

面犯桃花 2024-10-09 07:31:47

您是否将 CREATOR 包含在两者中?

public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
   public A createFromParcel(Parcel in) {
      return new A(in);
      }

   public A[] newArray(int size) {
      return new A[size];
      }
};

&接受 Parcel 的构造函数:

public A(Parcel parcel) {
   name = parcel.readString();
   //etc..
}

?

Did you include the CREATOR in for both?

public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
   public A createFromParcel(Parcel in) {
      return new A(in);
      }

   public A[] newArray(int size) {
      return new A[size];
      }
};

& constructors that take a Parcel:

public A(Parcel parcel) {
   name = parcel.readString();
   //etc..
}

?

鹤仙姿 2024-10-09 07:31:47
  1. List 更改为 ArrayList
  2. 使用 writeTypedListreadTypedList 写入和读取 ArrayList
  1. Change List<B> to ArrayList<B>
  2. Use writeTypedList and readTypedList to write and read ArrayList
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文