【问题标题】:Room Persistence: Entities and Pojos must have a usable public constructorRoom Persistence:实体和 Pojos 必须有一个可用的公共构造函数
【发布时间】:2019-05-21 07:44:19
【问题描述】:

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

错误:实体和 Pojos 必须有一个可用的公共构造函数。您可以有一个空的构造函数或参数与字段匹配的构造函数(按名称和类型)。 尝试了以下构造函数,但它们未能匹配: User(int,java.lang.String,java.lang.String,int,int,int,java.lang.String) -> [param:id -> 匹配字段:不匹配,参数:名称 -> 匹配字段:不匹配,参数:性别 -> 匹配字段:不匹配,参数:年龄 -> 匹配字段:不匹配,参数:体重 -> 匹配字段:不匹配,参数:高度 -> 匹配字段:不匹配,参数:锻炼 -> 匹配字段:不匹配]

这是我的代码:

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

} ...

谁能告诉我我做错了什么或我错过了什么?

【问题讨论】:

    标签: java android database


    【解决方案1】:

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

      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

    【讨论】:

    • 似乎没有必要让参数名称与属性名称完全匹配——我见过很多其他模式,我认为问题中的模式非常有效。你有理由这样做吗?
    • 从文档中可以看出,构造函数的参数包含与实体中的字段匹配的类型和名称。 developer.android.com/training/data-storage/room/defining-data
    • @pradipforever 非常有趣!谢谢你。这是新事物......通常变量名信息被丢弃,因此库不能依赖它,显然这不再是真的,一些库已经在利用这个事实!每天学习新东西。
    • 是的,没错,这个库利用了变量名。
    【解决方案2】:

    你也可以试试这个:-

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

    【讨论】:

    • 工作文件..!
    【解决方案3】:

    科特林:

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

    改为:

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

    【讨论】:

      【解决方案4】:

      我通过添加一个空的构造函数解决了同样的问题。 public User(){}

      【讨论】:

        【解决方案5】:

        我也遇到了这个错误。我的构造函数中只有一个属性不匹配,我发现它相当混乱。我的变量(在构造函数之外)被命名为 mURL,我试图将它与构造函数中的 url 匹配。

        相反,我应该将外部变量设置为 mUrl。是的,将最后两个字母大写给了我这个错误。设置this.mUrl = url 而不是this.mURL = url 解决了这个错误。

        【讨论】:

          【解决方案6】:

          就我而言,我解决了从默认构造函数中删除 @Ignore 的问题。

          如果您从某个地方复制代码,并且在默认构造函数中有 @Ignore,则需要从默认构造函数中删除 @Ignore

          之前:

          @Ignore
          public Card(){}
          

          之后:

          public Card(){}
          

          我刚刚添加,因为它发生在我身上。

          【讨论】:

            猜你喜欢
            • 2017-11-20
            • 1970-01-01
            • 1970-01-01
            • 2018-06-24
            • 1970-01-01
            • 2018-10-19
            • 2020-09-03
            • 2019-11-03
            • 2017-11-13
            相关资源
            最近更新 更多