2012-12-19 192 views
1

我想將一個對象從一個活動細節發送給人員活動。我跟着教程,但我得到以下錯誤:我無法將對象從一個活動轉移到另一個活動

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 

這裏是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]; 
     } 

    }; 
} 

這是詳細的活動,我想送Person對象。

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); 
     } 
    } 
}; 

這裏是我想獲取Person對象的Persons活動。

@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); 
} 

我能爲了解決這個問題呢?任何幫助表示感謝。提前感謝。

+0

[Bad Parcelable exception]的可能重複(http://stackoverflow.com/questions/12348627/bad-parcelable-exception) –

回答

4

你需要設置你的CREATOR公共靜態決賽:

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() 
+0

非常感謝,感謝您的幫助:) –

0

出於某種原因,我不能添加對Festus Tamakloe的回答發表評論,但我只想補充說這對我有幫助。我正在使用一些使用靜態創建器的教程。我沒有「公開」!

所以請確保你有公開。

相關問題