为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复“实体和 Pojos 必须有可用的公共构造函数”错误?

如何修复“实体和 Pojos 必须有可用的公共构造函数”错误?

大话西游666 2022-10-20 17:14:04
我正在尝试在我的 Android 应用程序中设置 Room。我收到此错误,但我真的不明白为什么。完全错误:实体和 Pojos 必须有一个可用的公共构造函数。您可以有一个空的构造函数或参数与字段匹配的构造函数(按名称和类型)。我正在使用 1.1.1 版的 Room。我尝试了空构造函数、带参数的构造函数,还修复了我遇到的所有警告,这样就没有其他问题了。这是我的实体:标记实体@Entity(tableName = "markers")public class Marker {    public Marker(@Nullable String title, @Nullable String snippet, String latitude, String longitude, float color) {        this.title = title;        this.snippet = snippet;        this.latitude = latitude;        this.longitude = longitude;        this.color = color;    }    @PrimaryKey(autoGenerate = true)    private int id;    private String title;    private String snippet;    private String latitude;    private String longitude;    private float color;    /* Getters and setters */}照片实体@Entity(tableName = "photos",        foreignKeys = @ForeignKey(entity = Marker.class,                                  parentColumns = "id",                                  childColumns = "marker_id"),        indices = {@Index("marker_id")})public class Photo {    public Photo(String imageBase64, int markerId) {        this.imageBase64 = imageBase64;        this.markerId = markerId;    }    @PrimaryKey(autoGenerate = true)    private int id;    @ColumnInfo(name = "image")    private String imageBase64;    @ColumnInfo(name = "marker_id")    private int markerId;    /* Getters and setters */}网络实体@Entity(tableName = "networks",        foreignKeys = @ForeignKey(entity = Marker.class,                                  parentColumns = "id",                                  childColumns = "marker_id"),        indices = {@Index("marker_id")})public class Network {    public Network(String ssid, String bssid, String capabilities, long timestamp, int markerId) {        this.ssid = ssid;        this.bssid = bssid;        this.capabilities = capabilities;        this.timestamp = timestamp;        this.markerId = markerId;    }}我看了很多这样的问题,但没有一个能解决我的问题。
查看完整描述

2 回答

?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

该错误是由于MarkerDao类中的错误而不是实体本身(不知何故)。

更准确地说,一个函数试图从查询中获取一个LatLng对象:SELECT string, string from ...

@Query("SELECT latitude, longitude FROM markers WHERE id = :id")
LatLng getMarkerLatitude(int id);

这个错误非常烦人,因为它没有提供任何细节。如果有人发现这一点,请注释掉您的实体和 DAO,并一次取消注释它们,以查看错误开始发生的位置。

祝你好运!

资料来源:我与@Jean-Christophe Martel 合作


查看完整回答
反对 回复 2022-10-20
?
达令说

TA贡献1821条经验 获得超6个赞

像这样创建空的构造函数。


public Marker(){};


public Photo(){};


public Network(){};

或者您可以删除所有构造函数。清理项目并重建。检查这是否有效。


查看完整回答
反对 回复 2022-10-20
  • 2 回答
  • 0 关注
  • 166 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号