6

我需要将一些数据发送到一个Activity,它可能在不同的上下文中运行。为此,我创建了一个类 A,它ArrayList的数据类型为 B 作为其实例成员之一。我将 B 类声明为 A 类的内部类。为了发送这个 A 类的实例Intent,我将 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 造成的。

有什么建议吗??

注意:当我使用android.os.Bundle.putParcelable(String key, Parcelable value)Bundle 类的函数,然后使用它解组它时,android.os.Bundle.getParcelable(String key)一切都很好。因此,问题似乎仅与意图有关。

4

2 回答 2

1

您是否在两者中都包含了 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];
      }
};

& 接受包裹的构造函数:

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

?

于 2011-10-03T15:00:50.257 回答
0
  1. 更改List<B>ArrayList<B>
  2. 使用writeTypedListandreadTypedList来读写 ArrayList
于 2014-04-15T04:02:55.000 回答