最近在写Android程序,当我实现了一个Draginfo类,继承Parcelable接口,Draginfo类有个属性id,类型为Integer,在writeToParcel调用用dest.writeInt(id),代码如下:
package edu.whu.sc.crawler.Disease; import android.os.Parcel; import android.os.Parcelable; public class Draginfo implements Parcelable { //设置为Integer则在后续将ArrayList<Draginfo>对象放入intent,传递给下一个activity时报错 private int id; private String name; private String nameused; private String nameused1; private String nameused3; private String nameused2; private String form; private String standard; private String indication; private String specification; private String reaction; private String other; private String other1; public Draginfo() { } public Draginfo(String name, String nameused, String nameused1, String nameused3, String nameused2, String form, String standard, String indication, String specification, String reaction, String other, String other1) { this.name = name; this.nameused = nameused; this.nameused1 = nameused1; this.nameused3 = nameused3; this.nameused2 = nameused2; this.form = form; this.standard = standard; this.indication = indication; this.specification = specification; this.reaction = reaction; this.other = other; this.other1 = other1; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getNameused() { return this.nameused; } public void setNameused(String nameused) { this.nameused = nameused; } public String getNameused1() { return this.nameused1; } public void setNameused1(String nameused1) { this.nameused1 = nameused1; } public String getNameused3() { return this.nameused3; } public void setNameused3(String nameused3) { this.nameused3 = nameused3; } public String getNameused2() { return this.nameused2; } public void setNameused2(String nameused2) { this.nameused2 = nameused2; } public String getForm() { return this.form; } public void setForm(String form) { this.form = form; } public String getStandard() { return this.standard; } public void setStandard(String standard) { this.standard = standard; } public String getIndication() { return this.indication; } public void setIndication(String indication) { this.indication = indication; } public String getSpecification() { return this.specification; } public void setSpecification(String specification) { this.specification = specification; } public String getReaction() { return this.reaction; } public void setReaction(String reaction) { this.reaction = reaction; } public String getOther() { return this.other; } public void setOther(String other) { this.other = other; } public String getOther1() { return this.other1; } public void setOther1(String other1) { this.other1 = other1; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeInt(id); dest.writeString(name); dest.writeString(nameused); dest.writeString(nameused1); dest.writeString(nameused2); dest.writeString(nameused3); dest.writeString(form); dest.writeString(standard); dest.writeString(indication); dest.writeString(specification); dest.writeString(reaction); dest.writeString(other); dest.writeString(other1); } public static final Parcelable.Creator<Draginfo> CREATOR = new Creator<Draginfo>() { @Override public Draginfo createFromParcel(Parcel source) { // TODO Auto-generated method stub Draginfo draginfo = new Draginfo(); draginfo.id = source.readInt(); draginfo.name = source.readString(); draginfo.nameused1 = source.readString(); draginfo.nameused2 = source.readString(); draginfo.nameused3 = source.readString(); draginfo.form = source.readString(); draginfo.standard = source.readString(); draginfo.indication = source.readString(); draginfo.specification = source.readString(); draginfo.reaction = source.readString(); draginfo.other = source.readString(); draginfo.other1 = source.readString(); return draginfo; } @Override public Draginfo[] newArray(int size) { // TODO Auto-generated method stub return new Draginfo[size]; } }; }
若我用Integer类型来设置id,则我在后面调用intent,将一个Arraylist<Draginfo>放入bundle里,再将bundle放入intent,则会报错。代码如下,其中bundle里面用bundle.putParcelableArrayList()放入了一个ArrayList<Draginfo>对象。
private void startShowActivity(Bundle bundle) { Intent intent = new Intent(getActivity(), ShowDrugInfoActivity.class); intent.putExtra("DragInfo", bundle);//这里会报错 startActivity(intent); }