[英]I can't transfer an object from one activity to another
I want to send an object from one Details Activity to Persons Activity. 我想将一个对象从一个Details活动发送到Persons活动。 I followed the tutorials but I get the following error:
我按照教程进行操作,但出现以下错误:
12-19 08:19:00.434: E/AndroidRuntime(1011): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workingonlist/com.example.workingonlist.Persons}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.workingonlist.Person
Here is the Person model: 这是Person模型:
public class Person implements Parcelable {
private String name;
private String surname;
private String gender;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(Parcel in){
name= in.readString();
surname= in.readString();
gender= in.readString();
}
public Person(String name,String surname,String gender){
this.name=name;
this.surname=surname;
this.gender=gender;
}
// getters and setters
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(surname);
dest.writeString(gender);
}
private Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Person(source);
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}
};
}
This is Details Activity which I want to send Person object. 这是我要发送Person对象的Details Activity。
private View.OnClickListener onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==btn_save.getId()){
Person p = new Person();
p.setName(et_name.getText().toString());
p.setSurname(et_surname.getText().toString());
switch(rg.getCheckedRadioButtonId()){
case R.id.button1:{
p.setGender("Male");
break;
}
case R.id.button2:{
p.setGender("Female");
break;
}
}
Intent i = new Intent(Details.this,Persons.class);
i.putExtra("person",p);
startActivity(i);
}
}
};
And here is the Persons Activity which I want to get Person object. 这是我想要获取Person对象的Persons Activity。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
PersonAdapter adapter = new PersonAdapter();
setContentView(R.layout.persons);
Bundle b = getIntent().getExtras();
if (b != null) {
Person p = b.getParcelable("person");
adapter.add(p);
}
setListAdapter(adapter);
}
What can I do in order to solve this problem ? 为了解决这个问题我该怎么办? Any help is appreciated.Thanks in advance.
感谢您的帮助。
您需要设置CREATOR公共静态final:
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
Look at this tutorials which deals with your issue 看一下处理您的问题的教程
public static final Parcelable.Creator CREATOR =
new Parcelable.Creator() {
public Person createFromParcel(Parcel in) {
return new Person(in);
}
public Person[] newArray(int size) {
return new Person[size];
}
};
http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/ http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
For some reason I can't add a comment to Festus Tamakloe's answer, but I just wanted to add that this helped me. 由于某种原因,我无法在Festus Tamakloe的答案中添加评论,但我只是想补充一点,这对我有所帮助。 I was following some tutorials which was using the static creator.
我正在关注一些使用静态创建器的教程。 I didn't have "public" on it!
我没有“公开”字样!
So make sure you have public on it. 因此,请确保您对此有兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.