gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:00 26 4
gpt4 key购买 nike

我正在尝试通过 Room Persistence 库将数据库添加到我的 Android 应用程序。在编译时出现上述错误。此外,Room 找不到我的 getter,尽管我可以在我的代码中清楚地看到它们。

这是我的实体代码:

@Entity(tableName = "users", indices = @Index(value = "username", unique = true))
public class User {

@NonNull
public String getuId() {
return uId;
}

public void setuId(@NonNull String uId) {
this.uId = uId;
}

public String getuUsername() {
return uUsername;
}

public void setuUsername(String uUsername) {
this.uUsername = uUsername;
}

@NonNull
public String getuPassword() {
return uPassword;
}

public void setuPassword(@NonNull String uPassword) {
this.uPassword = uPassword;
}

public String getuEmail() {
return uEmail;
}

public void setuEmail(String uEmail) {
this.uEmail = uEmail;
}

@NonNull
@PrimaryKey
@ColumnInfo(name = "user_id")
private String uId;

@ColumnInfo(name = "username")
private String uUsername;

@NonNull
@ColumnInfo(name = "password")
private String uPassword;

@ColumnInfo(name = "email")
private String uEmail;


@Ignore
public User(String userName, String email, String password){
uId = UUID.randomUUID().toString();
uUsername = userName;
uEmail = email;
uPassword = password;
}

public User(@NonNull String id, String username, String email,@NonNull String password){
this.uId = id;
this.uUsername = username;
this.uPassword = password;
this.uEmail = email;
}
}

我得到的错误:

Error:(14, 8) 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(java.lang.String,java.lang.String,java.lang.String,java.lang.String) : [id : null, username : null, email : null, password : null]

还有这个:

Error:(53, 20) error: Cannot find getter for field. Error:(56, 20) error: Cannot find getter for field. Error:(60, 20) error: Cannot find getter for field. Error:(63, 20) error: Cannot find getter for field.

最佳答案

根据 CommonsWare 和 rmlan 的建议,我的第一个问题是更改构造函数中的参数名称以匹配变量的名称。

getter 的问题在于 Room 使用 JavaBeans Conventions他们的名字,所以例如我的 getuUsername() 应该是 getUUsername()。在我更改了所有 getter 以匹配构建成功之后。

来源:Android Developers

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

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