房间持久性:实体和Pojos必须具有可用的公共构造函数

时间:2018-12-20 19:02:31

标签: java android database

我正在尝试通过Room Persistence库将数据库添加到我的android应用中,并且出现此错误:

  

错误:实体和Pojos必须具有可用的公共构造函数。您可以有一个空的构造函数或一个其参数与字段匹配的构造函数(按名称和类型)。       尝试了以下构造函数,但它们不匹配:       User(int,java.lang.String,java.lang.String,int,int,int,java.lang.String)-> [param:id->匹配字段:unmatched,param:name->匹配字段:unmatched ,param:gender->匹配字段:unmatched,param:age->匹配字段:unmatched,param:weight->匹配字段:unmatched,param:height->匹配字段:unmatched,param:workout->匹配字段:unmatched ]

这是我的代码:

    @Entity
    public class User {

@PrimaryKey
private int userId;
private String userName;
private String userGender;
private int userAge;
private int userWeight;
private int userHeight;
private String workoutPlan;


public User(int id, String name, String gender, int age, int weight, int height, String workout) {

    this.userId = id;
    this.userName = name;
    this.userGender = gender;
    this.userAge = age;
    this.userWeight = weight;
    this.userHeight = height;
    this.workoutPlan = workout;

} ...

有人可以告诉我我做错了什么或错过了什么吗?

6 个答案:

答案 0 :(得分:1)

请更改参数名称,使其与实体属性匹配。

  public User(int userId, String userName, String userGender, int userAge, int userWeight, int userHeight, String workoutPlan) {
    this.userId = userId;
    this.userName = userName;
    this.userGender = userGender;
    this.userAge = userAge;
    this.userWeight = userWeight;
    this.userHeight = userHeight;
    this.workoutPlan = workoutPlan;
  } ...

为了保持持久性,它在Room中使用JavaBeans约定。了解更多信息: https://developer.android.com/training/data-storage/room/defining-data#java

答案 1 :(得分:0)

您也可以尝试以下操作:-

@Ignore
public User(int id, String name, String gender, int age, int 
      weight, int height, String workout) {
   this.userId = id;
   this.userName = name;
   this.userGender = gender;
   this.userAge = age;
   this.userWeight = weight;
   this.userHeight = height;
   this.workoutPlan = workout;
 }

 public User(String name, String gender, int age, int 
      weight, int height, String workout) {
   this.userName = name;
   this.userGender = gender;
   this.userAge = age;
   this.userWeight = weight;
   this.userHeight = height;
   this.workoutPlan = workout;
 }

答案 2 :(得分:0)

我只是通过添加一个空的构造函数来解决这个问题。     public User(){}

答案 3 :(得分:0)

我也遇到了这个错误。我的构造函数中只有我的一个属性不匹配,我发现它相当混乱。我的变量(在构造函数之外)被命名为mURL,我试图将其与构造函数中的url匹配。

相反,我应该将外部变量设置为mUrl。是的,最后两个字母大写给了我这个错误。设置this.mUrl = url代替this.mURL = url解决了该错误。

答案 4 :(得分:0)

科特琳:

@Entity(tableName = "t_article_tabs")
data class WxArticleTabsEntity(
    @ColumnInfo(name = "tabId") @PrimaryKey @SerializedName("id") val id: Int?,
    @ColumnInfo(name = "tabName") @SerializedName("name") val name: String?,
    ...
    @Ignore  @SerializedName("children") val children: List<Any>?,
)

更改为:

@Entity(tableName = "t_article_tabs")
data class WxArticleTabsEntity(
    @ColumnInfo(name = "tabId") @PrimaryKey @SerializedName("id") val id: Int?,
    @ColumnInfo(name = "tabName") @SerializedName("name") val name: String?,
    ...
){
    @Ignore  @SerializedName("children") val children: List<Any>? = null
}

答案 5 :(得分:-1)

我解决了仅从默认构造函数中删除@Ignore的问题。

如果您从某个地方复制了代码,并在默认构造函数中使用了@Ignore,则需要从默认构造函数中删除@Ignore

之前:

@Ignore
public Card(){}

之后:

public Card(){}

我刚刚添加是因为它与我同在。