2017-06-17 122 views
1

實體和Pojos必須具有可用的公共構造函數。您可以有一個空的構造函數或構造函數,其參數與字段匹配(按名稱和類型)Room Android:實體和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; 
    } 
} 
+1

能否請您添加代碼 「模板」 類。我已經在沒有它的情況下測試過你的課程,它看起來可行。這個問題可能是由於嵌入ArrayList –

+0

@JackDalton感謝您的時間。我還包括模板和問題類。 – Jai

+0

的確如此,沒有它會奏效。但我無法一次修改整個代碼。只有增量更新。這就是爲什麼我想在團隊中問這個問題,以爲有人可能知道一些解決方法。 – Jai

回答

2

我不知道爲什麼你得到你的具體構造函數錯誤。這表示你的代碼將嵌入ArrayList錯誤。 @Embedded並不意味着以這種方式使用。 @Embedded允許您在存儲POJO結構時將其扁平化。嵌套的POJO屬性將顯示爲它們是父POJO上的屬性。在List上使用Embedded與要求它平整ArrayList對象的屬性並將它們存儲在一起,而不是壓扁列表項並存儲它們是一樣的。

適當的措施是過渡到外鍵,主鍵關係。另一種解決方案是創建一個新的POJO,其中包含您的項目列表(即模板,帶's')。這將包含一個模板對象的ArrayList。然後,您將定義一個轉換器,將POJO轉換爲json /逗號分隔列表,並將其存儲在默認情況下稱爲「模板」的單列中。這是一個鏈接到這種方法: Android room persistent library - TypeConverter error of error: Cannot figure out how to save field to database"

希望這可以幫助。

+0

是的。當我再次瀏覽文檔和視頻時,我明白了。但是我仍然感到困惑,外鍵方法是否正確。這有點像我的綠色信號。 – Jai

+0

更新了我的解決方案 –

+0

似乎很有希望。感謝您的鏈接。我試了一下。 – Jai

3

房間假設你的實體類將只有一個構造函數。但是,有沒有這樣的限制,如果你有那麼多個構造註釋其中一人

@Ignore

間會忽略這個構造,沒有任何錯誤編譯。

@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); 
} 
} 
相關問題