我正在嘗試在我的 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 合作

達令說
TA貢獻1821條經驗 獲得超6個贊
像這樣創建空的構造函數。
public Marker(){};
public Photo(){};
public Network(){};
或者您可以刪除所有構造函數。清理項目并重建。檢查這是否有效。
添加回答
舉報
0/150
提交
取消