Java 文件室持久性:实体和POJO必须具有可用的公共构造函数

Java 文件室持久性:实体和POJO必须具有可用的公共构造函数,java,android,database,Java,Android,Database,我试图通过Room Persistence library将数据库添加到我的android应用程序中,但出现以下错误: 错误:实体和POJO必须具有可用的公共构造函数。您可以有一个空构造函数,也可以有一个参数与字段(按名称和类型)匹配的构造函数。 尝试了以下构造函数,但未能匹配: 用户(int,java.lang.String,java.lang.String,int,int,int,java.lang.String)->[参数:id->匹配字段:不匹配,参数:姓名->匹配字段:不匹配,参数:性

我试图通过Room Persistence library将数据库添加到我的android应用程序中,但出现以下错误:

错误:实体和POJO必须具有可用的公共构造函数。您可以有一个空构造函数,也可以有一个参数与字段(按名称和类型)匹配的构造函数。 尝试了以下构造函数,但未能匹配: 用户(int,java.lang.String,java.lang.String,int,int,int,java.lang.String)->[参数:id->匹配字段:不匹配,参数:姓名->匹配字段:不匹配,参数:性别->匹配字段:不匹配,参数:年龄->匹配字段:不匹配,参数:体重->匹配字段:不匹配,参数:身高->匹配字段:不匹配,参数:训练->匹配字段:不匹配]

这是我的密码:

    @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;

} ...

有人能告诉我我做错了什么或遗漏了什么吗?

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

  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;
  } ...
对于持久性,它在文件室中使用JavaBeans约定。有关更多信息:

您也可以尝试以下方法:-

@Ignore
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;
 }

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

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

在我的例子中,我解决了从默认构造函数中删除
@Ignore
的问题

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

之前:

@Ignore
public Card(){}
public Card(){}
之后:

@Ignore
public Card(){}
public Card(){}

我之所以添加,是因为它发生在我身上。

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

相反,我应该将外部变量设置为mUrl。是的,将最后两个字母大写会导致该错误。设置
this.mUrl=url
而不是
this.mUrl=url
解决了该错误。

Kotlin:

@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>?,
)
@实体(tableName=“t\u article\u tabs”)
数据类WxArticleTabsEntity(
@ColumnInfo(name=“tabId”)@PrimaryKey@SerializedName(“id”)val id:Int?,
@ColumnInfo(name=“tabName”)@SerializedName(“name”)val name:String?,
...
@忽略@SerializedName(“子项”)val子项:列表?,
)
改为:

@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
}
@实体(tableName=“t\u article\u tabs”)
数据类WxArticleTabsEntity(
@ColumnInfo(name=“tabId”)@PrimaryKey@SerializedName(“id”)val id:Int?,
@ColumnInfo(name=“tabName”)@SerializedName(“name”)val name:String?,
...
){
@忽略@SerializedName(“子项”)val子项:列表?=null
}

似乎没有必要让参数名与属性名完全匹配——我见过很多其他模式,我认为问题中的模式非常有效。你对此有什么理由吗?从文档中可以看出,一个构造函数的参数包含的类型和名称与实体中字段的类型和名称相匹配。@pradipforever非常有趣!谢谢。这是一个新的东西…通常变量名信息被丢弃,因此它不能被库依赖,显然这不再是真的,一些库已经在利用这个事实了!每天学习一些新的东西。是的,没错,这个库正在利用变量names.工作文件。。!