【发布时间】:2019-11-12 21:22:48
【问题描述】:
我在这段代码中遇到了这个错误。 You must annotate primary keys with @NonNull. "uidString" is nullable. 当我用@NonNull 和@PrimaryKey 注释它时,An entity must have at least 1 field annotated with @PrimaryKey
怎么了?
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.util.UUID;
@Entity(tableName = "player_profiles")
public class PlayerGameProfile implements Parcelable {
@PrimaryKey
public String uidString = UUID.randomUUID().toString();
@ColumnInfo(name = "name")
public String name;
【问题讨论】:
-
When I annotate it with @NonNull, An entity must have at least 1 field annotated with @PrimaryKey你是用 both 还是只用@NonNull注释它? -
后者两者兼得。我也厌倦了更改订单
-
并确保导入正确的 NonNull 包:
import android.support.annotation.NonNull; -
是的,很抱歉粘贴了草率的代码,所有内容都存在。 OP已编辑
-
我已经尝试了你所做的,我没有收到任何编译或构建错误。问题出在其他地方,而不是注释。另外,我使用了 AndroidX 注释和 NOT 支持注释
标签: java android android-room