房间持久性:实体和Pojos必须具有可用的公共构造函数 [英] Room Persistence: Entities and Pojos must have a usable public constructor
问题描述
我正在尝试通过Room Persistence库将数据库添加到我的android应用中,并且出现此错误:
I am trying to add a database to my android app through the Room Persistence library and i am getting this error:
错误:实体和Pojos必须具有可用的公共构造函数。您可以有一个空的构造函数或一个其参数与字段匹配的构造函数(按名称和类型)。
尝试了以下构造函数,但未能匹配:
User(int,java.lang.String,java.lang.String,int,int,int,java.lang.String)-> [param :id->匹配字段:不匹配,参数:名称->匹配字段:不匹配,参数:性别->匹配字段:不匹配,参数:年龄->匹配字段:不匹配,参数:重量->匹配字段:不匹配,参数:高度->匹配字段:不匹配,参数:锻炼->匹配字段:不匹配]
error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). Tried the following constructors but they failed to match: User(int,java.lang.String,java.lang.String,int,int,int,java.lang.String) -> [param:id -> matched field:unmatched, param:name -> matched field:unmatched, param:gender -> matched field:unmatched, param:age -> matched field:unmatched, param:weight -> matched field:unmatched, param:height -> matched field:unmatched, param:workout -> matched field: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;
} ...
有人可以告诉我我在做什么错误或我错过了什么?
Can someone please tell me what i am doing wrong or what i missed?
推荐答案
请更改参数名称,使其与实体属性匹配。
Please change names of the parameters such that it matches entity attributes.
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
For persistance, it uses JavaBeans conventions in Room. For more information: https://developer.android.com/training/data-storage/room/defining-data#java
这篇关于房间持久性:实体和Pojos必须具有可用的公共构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!