首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >错误:实体和Pojos必须有一个可用的公共构造函数- Java

错误:实体和Pojos必须有一个可用的公共构造函数- Java
EN

Stack Overflow用户
提问于 2018-10-05 21:21:31
回答 2查看 5.5K关注 0票数 10

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
    @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:

代码语言:javascript
复制
@TypeConverters(ReplyMessages.class)

公共类ReplyMessage {

代码语言:javascript
复制
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:

代码语言:javascript
复制
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实体时,问题出现了。

编辑2:已通过移除ReplyMessage的List<>解决问题。虽然我使用的是转换器,但我不知道为什么列表不能工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-06 06:42:09

我已经解决了这个问题。似乎我不能将@Embedded与@TypeConverter一起使用,主要是因为转换器返回一个字符串,而且它是原语而不是POJO类。无论如何,我删除了@Embedded注解,它工作得很好。

票数 18
EN

Stack Overflow用户

发布于 2018-10-05 23:26:20

Android Room需要一个空的构造函数,因此从空的构造函数中移除@Ignore注释,并将其放在带参数的构造函数上。

代码语言:javascript
复制
 public SystemMessageEntity() {}

 @Ignore
 public SystemMessageEntity(String id, @NonNull String messageId, ...) {
     // ... initializing the params
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52666597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档