0

在我的项目中,我有一个实现 Parcelable 的类:

public class cursorParcel implements Parcelable{

    private long id;
    private long edc;
    private String name;
    private String notes;
    private long details;

    public cursorParcel() {
        id = 0;
        edc = 0;
        details = 0;
        notes = null;
        name = null;
    }
    public cursorParcel(Parcel in){
        id = in.readLong();
        edc= in.readLong();
        details= in.readLong();
        name= in.readString();
        notes=in.readString();
    }
    public void setCursor (Cursor cursor){
        id = cursor.getLong(1);
        edc = cursor.getLong(cursor.getColumnIndex("edc"));
        details = cursor.getLong(cursor.getColumnIndex("details"));
        notes = cursor.getString(cursor.getColumnIndex("notes"));
        name = cursor.getString(cursor.getColumnIndex("name"));
    }
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeLong(id);
        arg0.writeLong(edc);
        arg0.writeLong(details);
        arg0.writeString(name);
        arg0.writeString(notes);
    }
    public long getId(){
        return id;
    }
    public long getEdc(){
        return edc;
    }
    public long getDetails(){
        return details;
    }
    public String getName(){
        return name;
    }
    public String getNotes(){
        return notes;
    }
    public static final Parcelable.Creator<cursorParcel> CREATOR =  new Parcelable.Creator<cursorParcel>() {
            public cursorParcel createFromParcel(Parcel in) {
                return new cursorParcel(in);
            }
            public cursorParcel[] newArray(int size) {
                return new cursorParcel[size];
            }
    };  
}

我捆绑它,并将它传递给意图,当我打开它时,我得到一个异常。

Bundle bundle = this.getIntent().getExtras();
        if (bundle!=null){
            cursorParcel cursor =  bundle.getParcelable("cursor");
        }

此时我得到一个 ClassCastException 。请帮助我哪里出错了。

4

2 回答 2

0

尝试这个:

cursorParcel  mPerson = (cursorParcel)getIntent().getSerializableExtra("cursor");
于 2012-04-24T17:40:12.560 回答
-1

我认为您应该在 WriteToParcel 中将变量写为字符串:

arg0.writeStringArray(new String[]{String.valueOf(id),String.valueOf(edc),String.valueOf(details),this.name,this.notes});
于 2012-04-25T05:07:08.150 回答