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?
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
}
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With