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

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


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

}

回复消息.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;

}


慕姐4208626
浏览 664回答 2
2回答

qq_遁去的一_1

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

人到中年有点甜

Android Room 需要一个空的构造函数,因此@Ignore从空构造函数中删除注释并将其放置在带有参数的构造函数上。&nbsp;public SystemMessageEntity() {}&nbsp;@Ignore&nbsp;public SystemMessageEntity(String id, @NonNull String messageId, ...) {&nbsp; &nbsp; &nbsp;// ... initializing the params&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java