本文介绍了Android版|解组未知类型,而在捆绑使用Parcelable数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ArrayList与Parcelable数据问题通过使用Android.Bundle两项活动发送

我有两个活动(A和B)。

在Aaaa.class:

 的ArrayList<模型与GT; mModelList =新的ArrayList<模型与GT;
//填充的ArrayList与几个模型对象捆绑mBundle =新的捆绑;
意图mIntent =新意图(Aaaa.this,Bbbb.class);mBundle.putParcelableArrayList(模式,mModelList);
mIntent.putExtras(mBundle);startActivity(mIntent);

在Bbbb.class:

 捆绑mBundle = getIntent()getExtras()。
ArrayList的<模型与GT; = mBundle.getParcelableArrayList(模式);

该Model.class正在实施Parcelable。

所以,现在的问题是。
当我填写的ArrayList(以Aaaa.class),并把它捆,我可以看到包包含从列表中全面开花的对象。
当我再尝试填写Bbbb.class一个抛出异常的列表。

  ERROR / AndroidRuntime(11109):致命异常:主要
    了java.lang.RuntimeException:无法启动活动ComponentInfo {测试/ test.activities.Bbbb}了java.lang.RuntimeException:地块android.os.Parcel@405585d0:解组未知类型code 7667810偏移144
    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
    在android.os.Handler.dispatchMessage(Handler.java:99)
    在android.os.Looper.loop(Looper.java:123)
    在android.app.ActivityThread.main(ActivityThread.java:3687)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:507)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:842)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    在dalvik.system.NativeStart.main(本机方法)
    了java.lang.RuntimeException:地块android.os.Parcel@405585d0:由解组造成未知类型code 7667810偏移144
    在android.os.Parcel.readValue(Parcel.java:1913)
    在android.os.Parcel.readListInternal(Parcel.java:2092)
    在android.os.Parcel.readArrayList(Parcel.java:1536)
    在android.os.Parcel.readValue(Parcel.java:1867)
    在android.os.Parcel.readMapInternal(Parcel.java:2083)
    在android.os.Bundle.unparcel(Bundle.java:208)
    在android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
    在test.activities.Bbbb.onCreate(Bbbb.java:52)
    在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11更多

Line52是

 的ArrayList<模型与GT; = mBundle.getParcelableArrayList(模式);

我是绝对不知道哪里出了问题在这里,Model.class工作正常与其他捆绑 - 意图

更新:

模型类为其次。

 公共类模型实现Parceleble {  私人字符串名称;
  私人字符串的地址;  公共模型(邮包包裹){
  }
  公共模型(){
  }
  公共模型(字符串名称,地址字符串){
    this.name =名称;
    this.address =地址;
  }  // getter和setter  //等于,哈希code,ToString(从理念自动生成)  @覆盖
  公共无效writeToParcel(包裹包裹,int i)以{
    parcel.writeString(名);
    parcel.writeString(地址);
  }  公共无效readFromParcel(邮包包裹){
    this.name = parcel.readString();
    this.address = parcel.readString();
  }  公共静态Parcelable.Creator<模型与GT; CREATOR =新Parcelable.Creator<模型与GT;(){
  @覆盖
  众模特createFromParcel(邮包包裹){
    返回新模型(包裹);
  }  @覆盖
  众模特[]新的Array(INT大小){
    返回新模式[尺寸]
  }
};
}


解决方案

您Parceleble实现看起来不正确的我,的来看看需要什么样的方法/构造需要重写:

  ......// ==================== Parcelable ====================
公众诠释describeContents(){
  返回0;
}公共无效writeToParcel(包裹包裹,诠释标志){
  parcel.writeString(名);
  parcel.writeString(地址);
}私人模型(包裹中){
  名称= in.readString();
  地址= in.readString();
}公共静态最终Parcelable.Creator<模型与GT; CREATOR =新Parcelable.Creator<模型与GT;(){
  众模特createFromParcel(包裹中){
    返回新模型();
  }  众模特[] newArray(INT大小){
    返回新型号【尺寸】;
  }
};......

试试这个code和看看是否有帮助。

I have a Problem with Parcelable Data in an ArrayList sending via two Activities using Android.Bundle

I have two Activities (A and B).

In Aaaa.class:

ArrayList<Model> mModelList = new ArrayList<Model>
//Fill ArrayList with a few Model-Objects

Bundle mBundle = new Bundle;
Intent mIntent = new Intent(Aaaa.this, Bbbb.class);

mBundle.putParcelableArrayList("models", mModelList);
mIntent.putExtras(mBundle);

startActivity(mIntent);

In Bbbb.class:

Bundle mBundle = getIntent().getExtras();
ArrayList<Model> = mBundle.getParcelableArrayList("models");

The Model.class is implementing Parcelable.

So, the Problem is.When I fill the ArrayList (in Aaaa.class) and put it to the Bundle, I can see that the Bundle contains the varios Objects from the List.When I then try to fill the List in Bbbb.class a Exception is Thrown.

 ERROR/AndroidRuntime(11109): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{test/test.activities.Bbbb}: java.lang.RuntimeException: Parcel android.os.Parcel@405585d0: Unmarshalling unknown type code 7667810 at offset 144
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@405585d0: Unmarshalling unknown type code 7667810 at offset 144
    at android.os.Parcel.readValue(Parcel.java:1913)
    at android.os.Parcel.readListInternal(Parcel.java:2092)
    at android.os.Parcel.readArrayList(Parcel.java:1536)
    at android.os.Parcel.readValue(Parcel.java:1867)
    at android.os.Parcel.readMapInternal(Parcel.java:2083)
    at android.os.Bundle.unparcel(Bundle.java:208)
    at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
    at test.activities.Bbbb.onCreate(Bbbb.java:52)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more

Line52 is

ArrayList<Model> = mBundle.getParcelableArrayList("models");

I have absolutly no Idea where is the Problem here, the Model.class is working fine with other Bundle - Intents.

Update:

The Model Class as followed.

public class Model implements Parceleble{

  private String name;
  private String address;

  public Model(Parcel parcel){
  }
  public Model(){
  }
  public Model(String name, String address){
    this.name = name;
    this.address = address;
  }

  //Getter and Setter

  //equals, HashCode, toString (autoGenerated from Idea)

  @Override
  public void writeToParcel(Parcel parcel, int i){
    parcel.writeString(name);
    parcel.writeString(address);
  }

  public void readFromParcel(Parcel parcel){
    this.name = parcel.readString();
    this.address = parcel.readString();
  }

  public static Parcelable.Creator<Model> CREATOR = new Parcelable.Creator<Model>(){
  @Override
  public Model createFromParcel(Parcel parcel){
    return new Model(parcel);
  }

  @Override
  public Model[] new Array(int size){
    return new Model[size]
  }
};
}
解决方案

Your Parceleble implementation doesn't look quite correct to me, check out the sample code in API to see what are the required method/constructor that need to be overridden:

... ...

// ==================== Parcelable ====================
public int describeContents() {
  return 0;
}

public void writeToParcel(Parcel parcel, int flags) {
  parcel.writeString(name);
  parcel.writeString(address);
}

private Model(Parcel in) {
  name = in.readString();
  address = in.readString();
}

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

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

... ...

Try this code and see if it helps.

这篇关于Android版|解组未知类型,而在捆绑使用Parcelable数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:46