【问题标题】:Android: Exception Unmarshalling unknown typeAndroid:异常解组未知类型
【发布时间】:2012-07-31 06:17:27
【问题描述】:

我必须将 ArrayList 从一个活动传递到另一个活动。所以我用

intent.putParcelableArrayListExtra("Items",sendinglist);

我通过

得到了发送列表
ArrayList<GeoItem> revd = new ArrayList<GeoItem>();
        Bundle b = getIntent().getExtras();
        if (b != null)
            revd = b.getParcelableArrayList("Items");
Log.i("Element details",revd.get(0).getLatitude()+"");// Error 

但我无法访问该列表中的 GeoItem 对象。 更新基于答案的课程...

我的 GeoItem 类是

公共类 GeoItem 实现 Parcelable、Serializable {

/** id of item. */
protected long id_;
/** item location in GeoPoint. */
// protected GeoPoint location_;
/** selection state flag. true if selected. */
protected boolean isSelected_;
protected int latitude;
protected int longitude;
protected String incident_no;
protected String title;
protected String date;
protected String address;

/**
 * @param id
 *            item id.
 * @param latitudeE6
 *            latitude of the item in microdegrees (degrees * 1E6).
 * @param longitudeE6
 *            longitude of the item in microdegrees (degrees * 1E6).
 */
public GeoItem(long id, int latitudeE6, int longitudeE6, String inc_no,
        String tlt, String dates, String addr) {
    id_ = id;
    // location_ = new GeoPoint(latitudeE6, longitudeE6);
    isSelected_ = false;
    incident_no = inc_no;
    title = tlt;
    date = dates;
    address = addr;
    latitude=latitudeE6;
    longitude=longitudeE6;
}

public long getId_() {
    return id_;
}

public void setId_(long id_) {
    this.id_ = id_;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public void setIncident_no(String incident_no) {
    this.incident_no = incident_no;
}

public int getLatitude() {
    return latitude;
}

public void setLatitude(int latitude) {
    this.latitude = latitude;
}

public int getLongitude() {
    return longitude;
}

public void setLongitude(int longitude) {
    this.longitude = longitude;
}

/**
 * @param src
 *            source GeoItem
 */
public GeoItem(GeoItem src) {
    id_ = src.id_;
    // location_ = new
    // GeoPoint(src.location_.getLatitudeE6(),src.location_.getLongitudeE6());
    isSelected_ = src.isSelected_;
}

/**
 * @param src
 *            source Parcel
 */
public GeoItem(Parcel src) {
    id_ = src.readLong();
    // location_ = new GeoPoint(src.readInt(), src.readInt());
    isSelected_ = src.readInt() == 0 ? false : true;
    address = src.readString();
    date = src.readString();
}

/* describeContents */
public int describeContents() {
    return 0;
}

/**
 * getId
 * 
 * @return id of the item.
 */
public long getId() {
    return id_;
}

public String getIncident_no() {
    return incident_no;
}

/**
 * setId
 * 
 * @param id
 *            of the item.
 */
public void setId(long id) {
    id_ = id;
    ;
}

/**
 * getLocation
 * 
 * @return GeoPoint of the item.
 * 
 *         public GeoPoint getLocation() { return location_; }
 */
/**
 * isSelected
 * 
 * @return true if the item is in selected state.
 */
public boolean isSelected() {
    return isSelected_;
}

/**
 * setSelect
 * 
 * @param flg
 *            flag to be set.
 */
public void setSelect(boolean flg) {
    isSelected_ = flg;
}

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

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

/**
 * writeToParcel
 * 
 * @param parcel
 *            Parcel to be written.
 * @param flags
 *            flag.
 */
public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeLong(id_);
    parcel.writeString(address);
    parcel.writeString(date);
    parcel.writeInt(latitude);
    parcel.writeInt(longitude);
    int flg = isSelected_ ? 1 : 0;
    parcel.writeInt(flg);
}


}

请给我最好的方法...

【问题讨论】:

    标签: java android android-intent


    【解决方案1】:

    GeoPoint 不是Parceable,因此它可以被编组。您可能希望以某种方式保存数据而不使用GeoPoint 或扩展GeoPoint,以便实现Parceable

    【讨论】:

    • 我已经从我的 GeoItem 类中删除了 GeoPoint,而是添加了 2 个成员,例如纬度和经度。但还是同样的错误
    【解决方案2】:
    **To pass an arraylist  of Category to another activity,** 
    
    
    
     intent i = new Intent(_av.getContext(), ItemList.class);
        Bundle b = new Bundle();
        b.putParcelableArrayList("categories", categories);
        b.putInt("index", _index);
        i.putExtras(b);
        startActivityForResult(i, ITEM_LIST);
    
    **To retrieve the data,**
    
    Bundle b = this.getIntent().getExtras();
    
    ArrayList<Category> cats = b.getParcelableArrayList("categories");
    int index = b.getInt("index");
    

    【讨论】:

      【解决方案3】:

      我的实际任务是通过意图发送 GeoItems 对象列表。我的 GeoItems 类也是可序列化的。显然可序列化对象的列表也是serialibale所以我用了,

      Intent listview = new Intent(context, ReportList.class);
                  Bundle send = new Bundle();
                  send.putSerializable("Items", (Serializable) items);
                  listview.putExtras(send);
                  context.startActivity(listview);
      

      其中 items 是 GeoIems 对象的列表

      在接收活动时,

       Bundle b = this.getIntent().getExtras();
              if (b != null) {
                  data = (List<GeoItem>) b.getSerializable("Items");
              }
      

      其中 data 是 GeoIems 对象的列表

      不使用 Parceable 接口,现在我可以通过意图设置我的可序列化对象列表到下一个活动

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-08
        • 2020-06-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-24
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        相关资源
        最近更新 更多