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

ajo*_*onk 6 java database android

我正在尝试通过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 ,参数:锻炼->匹配字段:不匹配]

这是我的代码:

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

} ...
Run Code Online (Sandbox Code Playgroud)

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

Pra*_*rki 6

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

  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;
  } ...
Run Code Online (Sandbox Code Playgroud)

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