在 Android 中创建 Parcelable 类时遇到问题
我正在尝试在 Android 中创建一个 Parcelable 类,以便我可以在 Activity 之间传递这些对象的 ArrayList。我非常密切地关注了 StackOverflow (http://stackoverflow.com/questions/6201311/how-to-read-write-a-boolean-when-implementing-the-parcelable-interface) 和Android 文档(http://developer.android.com/reference/android/os/Parcelable.html),但我在所需的静态字段 CREATOR 上不断收到错误:字段 CREATOR 不能声明为静态;静态字段只能在静态或顶级类型中声明
我在我的类以及直接从 Android 文档剪切/粘贴的示例类上收到此错误。我的情况一定有一些我不知道的独特之处......?我的课程如下。有什么想法吗?
谢谢,
布莱恩
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];
}
};
}
I'm attempting to create a Parcelable class in Android so that I can pass a ArrayList of these objects between Activities. I've followed very closely the examples found here on StackOverflow (http://stackoverflow.com/questions/6201311/how-to-read-write-a-boolean-when-implementing-the-parcelable-interface)and in the Android docs (http://developer.android.com/reference/android/os/Parcelable.html), but I keep getting an error on the required static field CREATOR: The field CREATOR cannot be declared static; static fields can only be declared in static or top level types
I get this error on my class, as well as the example class cut/pasted directly from the Android docs. There must be something unique to my situation that I'm not aware of....?? My class is given below. Any ideas?
Thanks,
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];
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你的类在另一个类中被声明为非静态,这就是为什么它不允许有静态数据成员。要么将其设为顶级类,要么将其声明为静态。
Sounds like your class is being declared non-static inside another class, which is why it’s not allowed to have static data members. Either make it a top-level class, or declare it static.