android parcelable异常

时间:2012-04-24 17:25:52

标签: android exception bundle parcelable

在我的项目中,我有一个实现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];
            }
    };  
}

我捆绑它,并将其传递给intent,当我解压缩它时,我得到一个例外。

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

此时我收到了ClassCastException。 在我出错的地方帮助我。

2 个答案:

答案 0 :(得分:0)

试试这个:

cursorParcel  mPerson = (cursorParcel)getIntent().getSerializableExtra("cursor");

答案 1 :(得分:-1)

我认为你应该把你的变量写成WriteToParcel中的字符串:

arg0.writeStringArray(new String[]{String.valueOf(id),String.valueOf(edc),String.valueOf(details),this.name,this.notes});