gpt4 book ai didi

java - 房间持久性 : Entities and Pojos must have a usable public constructor

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:31 25 4
gpt4 key购买 nike

我正在尝试通过 Room Persistence 库将数据库添加到我的 Android 应用程序,但出现此错误:

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;

} ...

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

最佳答案

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

  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

关于java - 房间持久性 : Entities and Pojos must have a usable public constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53874559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com