实体和Pojos必须具有可用的公共构造函数。您可以拥有一个空构造函数或构造函数,其参数与字段(按名称和类型)匹配
将房间整合到我现有的项目中。在使用@Entity标记注释实现Parcelable的POJO并进行必要的更改时,会收到此错误。我已经有一个空的构造函数。任何帮助将不胜感激。
@Entity(tableName = "Departments")
public class Department implements Parcelable {
@PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String departmentName;
private String logoUrl;
@Embedded
private ArrayList<Template> templateList;
public Department() {
}
protected Department(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.departmentName = in.readString();
this.logoUrl = in.readString();
this.id = (Integer) in.readSerializable();
this.templateList = in.createTypedArrayList(Template.CREATOR);
}
public static final Creator<Department> CREATOR = new Creator<Department>() {
@Override
public Department createFromParcel(Parcel in) {
return new Department(in);
}
@Override
public Department[] newArray(int size) {
return new Department[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(primaryId);
dest.writeString(departmentName);
dest.writeString(logoUrl);
dest.writeSerializable(id);
dest.writeTypedList(templateList);
}
public Integer getPrimaryId() {
return primaryId;
}
public void setPrimaryId(Integer primaryId) {
this.primaryId = primaryId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLogoUrl() {
return logoUrl;
}
public void setLogoUrl(String logoUrl) {
this.logoUrl = logoUrl;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public ArrayList<Template> getTemplateList() {
return templateList;
}
public void setTemplateList(ArrayList<Template> templateList) {
this.templateList = templateList;
}
}
@Entity(tableName = "Templates")
public class Template implements Parcelable {
@PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String code;
private String description;
private Integer departmentId;
@Embedded
private ArrayList<Issue> issueList;
public Template() {
}
private Template(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.code = in.readString();
this.description = in.readString();
this.id = (Integer) in.readSerializable();
this.departmentId = (Integer) in.readSerializable();
this.issueList = in.createTypedArrayList(Issue.CREATOR);
}
public static final Creator<Template> CREATOR = new Creator<Template>() {
@Override
public Template createFromParcel(Parcel in) {
return new Template(in);
}
@Override
public Template[] newArray(int size) {
return new Template[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(primaryId);
dest.writeString(code);
dest.writeString(description);
dest.writeSerializable(id);
dest.writeSerializable(departmentId);
dest.writeTypedList(issueList);
}
public Integer getPrimaryId() {
return primaryId;
}
public void setPrimaryId(Integer primaryId) {
this.primaryId = primaryId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ArrayList<Issue> getIssueList() {
return issueList;
}
public void setIssueList(ArrayList<Issue> issueList) {
this.issueList = issueList;
}
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
}
@Entity(tableName = "Issues")
public class Issue implements Parcelable {
@PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String code;
private String description;
private Integer parentIssue;
public Issue() {
}
protected Issue(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.code = in.readString();
this.description = in.readString();
this.id = (Integer) in.readSerializable();
this.parentIssue = (Integer) in.readSerializable();
}
public static final Creator<Issue> CREATOR = new Creator<Issue>() {
@Override
public Issue createFromParcel(Parcel in) {
return new Issue(in);
}
@Override
public Issue[] newArray(int size) {
return new Issue[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(primaryId);
dest.writeString(code);
dest.writeString(description);
dest.writeSerializable(id);
dest.writeSerializable(parentIssue);
}
public Integer getPrimaryId() {
return primaryId;
}
public void setPrimaryId(Integer primaryId) {
this.primaryId = primaryId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getParentIssue() {
return parentIssue;
}
public void setParentIssue(Integer parentIssue) {
this.parentIssue = parentIssue;
}
}
答案 0 :(得分:6)
Room假设您的实体类只有一个构造函数。但是没有这样的限制,如果你有多个构造函数,那么用
注释其中一个@Ignore
Room将忽略此构造函数并进行编译而不会出现任何错误。
示例强>
@Entity(tableName = "Departments")
public class Department implements Parcelable {
@PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String departmentName;
private String logoUrl;
@Embedded
private ArrayList<Template> templateList;
/**Room will ignore this constructor
**/
@Ignore
public Department() {
}
protected Department(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.departmentName = in.readString();
this.logoUrl = in.readString();
this.id = (Integer) in.readSerializable();
this.templateList = in.createTypedArrayList(Template.CREATOR);
}
}
答案 1 :(得分:2)
我不确定您为什么会收到特定的构造函数错误。这说明嵌入ArrayList会导致代码错误。 @Embedded不应该以这种方式使用。 @Embedded允许您在存储时展平您的POJO结构。嵌套的POJO属性将显示为它们是父POJO上的属性。在List上使用Embedded与要求它展平ArrayList对象的属性并存储它们相同,而不是展平列表项并存储它们。
适当的措施是转换为外键,主键关系。另一种解决方案是创建一个包含项目列表的新POJO(即模板,带有's')。这将包含Template对象的ArrayList。然后,您将定义一个转换器,将POJO转换为json /逗号分隔列表,并将其存储在单个列中,默认情况下将其称为“模板”。以下是此方法的链接: Android room persistent library - TypeConverter error of error: Cannot figure out how to save field to database"
希望这有帮助。