Я пытаюсь создать класс Parcelable на Android, чтобы передать ArrayList из этих объектов между Activity. Я внимательно следил за примерами, найденными здесь, в StackOverflow (http://stackoverflow.com/questions/6201311/how-to-read-write-a-boolean-when-implementing-the-parcelable-interface) и в Android docs (http://developer.android.com/reference/android/os/Parcelable.html), но я продолжаю получать ошибку в требуемом статическом поле CREATOR: Поле CREATOR не может быть объявлено статическим; статические поля могут быть объявлены только в типах статического или верхнего уровня.Проблема с созданием Parcelable Class в Android
Я получаю эту ошибку в своем классе, а также класс класса, вырезанный/вставленный непосредственно из документов Android. Должно быть что-то уникальное для моей ситуации, о котором я не знаю .... ?? Мой класс приведен ниже. Есть идеи?
Спасибо,
Bryan
public class ComplaintType implements Parcelable
{
// class data
private int groupID = 0;
private int id = 0;
private String description = "";
private boolean checked = false; // does this complaint type apply to this patient?
// constructors
public ComplaintType() {}
public ComplaintType(int _groupID, String desc, int _id, boolean ckd) {
this.groupID = _groupID;
this.description = desc;
this.id = _id;
this.checked = ckd;}
// getters/setters
public int getGroupID() {return groupID;}
public void setGroupID(int _groupID) { this.groupID = _groupID;}
public String getDesc() {return description;}
public void setDesc(String desc) {this.description = desc;}
public int getID() {return id;}
public void setID(int _id) {this.id = _id;}
public boolean isChecked() {return checked;}
public void setChecked(boolean ckd) {this.checked = ckd;}
// utility functions
public String toString() {return this.description;}
public void toggleChecked() {checked = !checked;}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(groupID);
dest.writeInt(id);
dest.writeString(description);
dest.writeByte((byte) (checked ? 1 : 0)); // convert byte to a boolean (1=true, 0=false)
}
public static final Parcelable.Creator<ComplaintType> CREATOR // <-- ERROR OCCURS HERE
= new Parcelable.Creator<ComplaintType>() {
public ComplaintType createFromParcel(Parcel in){
ComplaintType complaint = new ComplaintType();
complaint.setGroupID(in.readInt());
complaint.setID(in.readInt());
complaint.setDesc(in.readString());
complaint.setChecked(in.readByte() == 1); // store the boolean as a byte (1=true, 0=false)
return complaint;
}
@Override
public ComplaintType[] newArray(int size) {
return new ComplaintType[size];
}
};
}
Doh! Вы были совершенно правы; Я объявил это в другом классе. Сделал класс высшего уровня ... проблема ушла. Теперь я чувствую себя немного глупо. Спасибо за толкание, Лоуренс. –