简体   繁体   中英

Room Persistence: Entities and Pojos must have a usable public constructor

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?

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

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>?,
)

Change to:

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

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM