gpt4 book ai didi

android - 在 Android 中创建 Parcelable 类时遇到问题

转载 作者:行者123 更新时间:2023-11-29 16:16:28 27 4
gpt4 key购买 nike

我正在尝试在 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 上不断收到错误消息:The field CREATOR cannot be declared static;静态字段只能在静态或顶级类型中声明

我在类里面遇到了这个错误,示例类也是直接从 Android 文档中剪切/粘贴的。我的情况一定有一些我不知道的独特之处……??我的类(class)如下。有什么想法吗?

谢谢,

布莱恩

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

}

最佳答案

听起来你的类在另一个类中被声明为非静态的,这就是为什么它不允许有静态数据成员。要么将其设为顶级类,要么将其声明为静态。

关于android - 在 Android 中创建 Parcelable 类时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9089292/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com