【问题标题】:error: Entities and Pojos must have a usable public constructor - Java错误:实体和 Pojos 必须有一个可用的公共构造函数 - Java
【发布时间】:2018-10-05 13:21:30
【问题描述】:

每当我尝试编译应用程序时都会收到此错误

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). - java.util.List

SystemMessagesEntity.java:

    @Entity(tableName = "system_messages")
    @TypeConverters(ReplyMessages.class)
    public class SystemMessageEntity {
        @PrimaryKey
        @NonNull
        public String messageId;
        public String title;
        public String body;
        public String color;
        public Integer date;
        public String icon;
        public String ad;
        public Integer isRead;
        public String ip;
        String id; //user_id
        @Embedded(prefix = "reply_")
        private List<ReplyMessage> reply_message;
    
        @Ignore
        public SystemMessageEntity() {
        }
    
        public SystemMessageEntity(String id, @NonNull String messageId, String title, String body, String color, Integer date, String icon, String ad, Integer isRead, String ip, List<ReplyMessage> reply_message) {
            this.id = id;
            this.messageId = messageId;
            this.title = title;
            this.body = body;
            this.color = color;
            this.date = date;
            this.icon = icon;
            this.ad = ad;
            this.isRead = isRead;
            this.ip = ip;
            this.reply_message = reply_message;
        }
    //getters and setters
}

ReplyMessages.java:

@TypeConverters(ReplyMessages.class)

公共类ReplyMessage {

private String body = "";
private String userId = "";
private Integer date = 0;
private String id = "";
private String messageId = "";


@Ignore
public ReplyMessage() {
}

public ReplyMessage(String body, String userId, Integer date, String id, String messageId) {
    this.body = body;
    this.date = date;
    this.id = id;
    this.messageId = messageId;
    this.userId = userId;
}

这是 TypeConverter :

public class ReplyMessages {

    @TypeConverter
    public static String ListToJson(List<ReplyMessage> replyMessages) {
        if(replyMessages == null) return null;
        Type type = new TypeToken<List<ReplyMessage>>() {}.getType();
        String json = new Gson().toJson(replyMessages, type);
        Log.i("JSON", "toJson: " + json);
        return replyMessages.isEmpty() ? null : json;
    }

    @TypeConverter
    public static List<ReplyMessage> JsonToList(String json) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<ReplyMessage>>() {}.getType();
        List<ReplyMessage> replyMessages = gson.fromJson(json, type);
        return replyMessages;
    }
}

我想使用 Retrofit 从 API 获取 JSON 数据并将其存储在数据库中,JSON 数据在父对象数组 (SystemMessagesEntity) 中有一个对象数组 (MessageReply)。

我已经尝试了所有方法并在 StackOverflow 中搜索了几乎所有问题,但我没有找到任何东西。

编辑:当我单独使用SystemMessageEntity 实体时,数据库运行良好。但是当我添加ReplyMessage Entity时,问题就出现了。

编辑 2:问题已通过删除回复消息的List&lt;&gt; 得到解决。尽管我正在使用转换器,但我不知道为什么该列表不起作用。

【问题讨论】:

  • 只是一个疯狂的猜测,但这不是@Ignore注解导致反射逻辑找不到空构造函数吗?
  • @PiotrWilkin 即使它不存在,也会发生同样的问题。

标签: java android android-room pojo


【解决方案1】:

我已经解决了这个问题。好吧,似乎我不能将@Embedded 与@TypeConverter 一起使用,主要是因为转换器返回一个String,它是原始的而不是POJO 类。无论如何,我删除了@Embedded 注释,它工作正常。

【讨论】:

  • 这也是我的问题。
  • 保存了我的最后期限兄弟!
  • @NaveenKumawat 很高兴我能提供帮助。
【解决方案2】:

Android Room 需要一个空的构造函数,所以将空构造函数中的@Ignore注解去掉,并将其放在带参数的构造函数上。

 public SystemMessageEntity() {}

 @Ignore
 public SystemMessageEntity(String id, @NonNull String messageId, ...) {
     // ... initializing the params
 }

【讨论】:

  • 你在两个 POJO 类上都试过了吗?这是最简单和推荐的方法,即将@Ignore 注释放在每个 POJO 类不为空/空的构造函数上。如果您仍然收到错误,请发布完整的 logcat 错误消息,以便其他人也可以提供帮助。
  • 问题已通过移除ReplyMessage的List解决。尽管我正在使用转换器,但我不知道为什么该列表不起作用。
猜你喜欢
  • 2019-11-03
  • 2017-11-20
  • 2019-05-21
  • 1970-01-01
  • 2019-09-14
  • 2017-11-13
  • 2018-10-19
  • 1970-01-01
  • 2018-03-17
相关资源
最近更新 更多