0

嗨,我有一个像这样实现 Parcelable 的类:

public class adapterSource implements Parcelable {
   String adiSoyadi;
   String isemriTipi;
   String ililce;
   String mahalle;
   String saat;
   String durum;

   private List<adapterSource> s;

   public adapterSource(String adiSoyadi, String isemriTipi,String ililce, String mahalle, String saat, String durum ) {
      this.adiSoyadi = adiSoyadi;
      this.isemriTipi = isemriTipi;
      this.ililce = ililce; 

      this.mahalle = mahalle;

      this.saat = saat;
      this.durum = durum;
   }

   public adapterSource(Parcel in) {
      s = new ArrayList<adapterSource>();
      in.readList(s, in.getClass().getClassLoader());
      adiSoyadi=in.readString();
      isemriTipi=in.readString();
      ililce=in.readString();
      mahalle=in.readString();
      saat=in.readString();
      durum=in.readString();
   }

   public int describeContents() {
      return 0;
   }

   public void writeToParcel(Parcel arg0, int arg1) {
   }

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

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

我正在尝试在这样的一项活动中创建此类的一个实例:

adapterSource[] source = new adapterSource[7];
source[0] = (adapterSource) new adapterSource("isa", "isa", "asd", "asd", "asd", "asd");
source[1] = new adapterSource("isa", "isa", "asd", "asd", "asd", "asd");
source[2] = new adapterSource("isa", "isa", "asd", "asd", "asd", "asd");
source[3] = new adapterSource("isa", "isa", "asd", "asd", "asd", "asd");
source[4] = new adapterSource("isa", "isa", "asd", "asd", "asd", "asd");
source[5] = new adapterSource("isa", "isa", "asd", "asd", "asd", "asd");
source[6] = new adapterSource("isa", "isa", "asd", "asd", "asd", "asd");
Intent in = new Intent().setClass(this, MainListActivity.class);

in.putExtra("source", source);

startActivity(in);

我正在从另一个这样的活动中打电话:

adapterSource[] ada= (adapterSource[]) this.getIntent().getParcelableArrayExtra("source");

我得到了解组错误。问题是什么。请帮忙...

4

1 回答 1

0

肯定你错过或忘记了实现writeToParcel(),实际的对象序列化/扁平化发生在这里。您需要单独包裹对象的每个元素。

第二件事,您in.readList();以错误的方式使用,如果可包裹对象包含自定义对象列表,您需要使您的自定义类也可包裹并且需要使用 -in.readList();

那么你的代码应该像

public class adapterSource implements Parcelable {
    String adiSoyadi;
    String isemriTipi;
    String ililce;
    String mahalle;
    String saat;
    String durum;

    public adapterSource(String adiSoyadi, String isemriTipi, String ililce,
            String mahalle, String saat, String durum) {
        this.adiSoyadi = adiSoyadi;
        this.isemriTipi = isemriTipi;
        this.ililce = ililce;
        this.mahalle = mahalle;
        this.saat = saat;
        this.durum = durum;
    }

    public adapterSource(Parcel in) {
        adiSoyadi = in.readString();
        isemriTipi = in.readString();
        ililce = in.readString();
        mahalle = in.readString();
        saat = in.readString();
        durum = in.readString();
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeString(adiSoyadi);
        arg0.writeString(isemriTipi);
        arg0.writeString(ililce);
        arg0.writeString(mahalle);
        arg0.writeString(saat);
        arg0.writeString(durum);
    }

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

        public adapterSource[] newArray(int size) {
            return new adapterSource[size];
        }
    };
}
于 2012-08-14T16:39:31.970 回答