gpt4 book ai didi

java - 错误 : Entities and Pojos must have a usable public constructor - Java

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:22 26 4
gpt4 key购买 nike

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

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

系统消息实体.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)

公共(public)类 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;
}

这是类型转换器:

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 数据在父对象数组 ( MessageReply ) 中有一个对象数组 ( SystemMessagesEntity )。

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

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

编辑 2:问题已通过删除 List<> 得到解决的回复消息。虽然我正在使用转换器,但我不知道为什么列表不起作用。

最佳答案

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

关于java - 错误 : Entities and Pojos must have a usable public constructor - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52666597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com