亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復“實體和 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 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號