Room Persistence: Entities and Pojos must have a u

2020-06-03 09:10发布

I am trying to add a database to my android app through the Room Persistence library and i am getting this error:

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]

Here is my code:

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

5条回答
一夜七次
2楼-- · 2020-06-03 09:26

I also got this error. Only one of my attributes in my constructor did not match and I was finding it rather confusing. My variable (outside the constructor) was named mURL and I was trying to match it to url within the constructor.

Instead, I should have set the outer variable to mUrl. Yes, capitalising the last 2 letters gave me that error. Setting this.mUrl = url instead of this.mURL = url solved the error.

查看更多
我只想做你的唯一
3楼-- · 2020-06-03 09:37

In my case, I solved problem just removing @Ignore from default constructor.

If you copied your code from some place, and have @Ignore in default constructor then need to remove @Ignore from default constructor.

Before:

@Ignore
public Card(){}

After:

public Card(){}

I just added because it's happen with me.

查看更多
我只想做你的唯一
4楼-- · 2020-06-03 09:41

you can also try this:-

@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;
 }
查看更多
时光不老,我们不散
5楼-- · 2020-06-03 09:42

I got around this same problem by just adding a empty constructor. public User(){}

查看更多
The star\"
6楼-- · 2020-06-03 09:46

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;
  } ...

For persistance, it uses JavaBeans conventions in Room. For more information: https://developer.android.com/training/data-storage/room/defining-data#java

查看更多
登录 后发表回答