Java 有多个好的构造函数,Room将选择无参数构造函数
我只有一个构造函数,但我不知道为什么编译器总是说“有多个好的构造函数,Room会选择无参数构造函数。” 我在迁移到Kotlin的同时使用java和Kotlin,但对它不太熟悉 嵌入的BluetoothInfo类在Kotlin中,而MarkerEntity类在java中 MarkerEntity.javaJava 有多个好的构造函数,Room将选择无参数构造函数,java,android,kotlin,local-storage,Java,Android,Kotlin,Local Storage,我只有一个构造函数,但我不知道为什么编译器总是说“有多个好的构造函数,Room会选择无参数构造函数。” 我在迁移到Kotlin的同时使用java和Kotlin,但对它不太熟悉 嵌入的BluetoothInfo类在Kotlin中,而MarkerEntity类在java中 MarkerEntity.java @Entity(tableName = "marker_table", indices = {@Index(value = "light_id", unique = true)}) public
@Entity(tableName = "marker_table", indices = {@Index(value = "light_id", unique = true)})
public class MarkerEntity {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "light_id")
private int lightId;
@ColumnInfo(name = "lat")
private int x;
@ColumnInfo(name = "lng")
private int y;
@ColumnInfo(name = "title")
private String title = "Default LightMarker";
@ColumnInfo(name = "description")
private String description = "No Description";
@Embedded
private BluetoothInfo bluetoothInfo;
public MarkerEntity(int lightId, int x, int y, String title, String description, BluetoothInfo bluetoothInfo) {
this.lightId = lightId;
this.x = x;
this.y = y;
if (title != null) this.title = title;
if (description != null) this.description = description;
if (bluetoothInfo != null) {
this.bluetoothInfo = bluetoothInfo;
} else {
bluetoothInfo = new BluetoothInfo();
}
}
public int getId() {
return id;
}
public int getLightId() {
return lightId;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BluetoothInfo getBluetoothInfo() {
return bluetoothInfo;
}
public void setLightId(int light_id) {
this.lightId = light_id;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setBluetoothInfo(BluetoothInfo bluetoothInfo) {
this.bluetoothInfo = bluetoothInfo;
}
@Override
public String toString() {
return lightId + " " + x + " " + y + " " + title + " " + description + "\n" + bluetoothInfo.toString();
}
}
public class BluetoothInfo {
private static final int INVALID_RSSI = 99;
private static final float INVALID_DIST = -1f;
private static final String INVALID_BT_STRING = "invalid";
@ColumnInfo(name = "bluetooth_name")
private String bluetoothName;
@ColumnInfo(name = "bluetooth_addr")
private String bluetoothAddress;
@ColumnInfo(name = "bluetooth_uuid")
private String bluetoothUuid;
@ColumnInfo(name = "bluetooth_major")
private String bluetoothMajor;
@ColumnInfo(name = "bluetooth_minor")
private String bluetoothMinor;
@ColumnInfo(name = "rssi")
private int rssi;
@ColumnInfo(name = "distance")
private float distance;
public BluetoothInfo(String bluetoothName, String bluetoothAddress, String bluetoothUuid, String bluetoothMajor, String bluetoothMinor, int rssi, float distance){
this.bluetoothName = bluetoothName;
this.bluetoothAddress = bluetoothAddress;
this.bluetoothUuid = bluetoothUuid;
this.bluetoothMajor = bluetoothMajor;
this.bluetoothMinor = bluetoothMinor;
this.rssi = rssi;
this.distance = distance;
}
public String getBluetoothName() {
return bluetoothName;
}
public String getBluetoothAddress() {
return bluetoothAddress;
}
public String getBluetoothUuid() {
return bluetoothUuid;
}
public String getBluetoothMajor() {
return bluetoothMajor;
}
public String getBluetoothMinor() {
return bluetoothMinor;
}
public int getRssi() {
return rssi;
}
public float getDistance() {
return distance;
}
public void setBluetoothName(String bluetoothName) {
this.bluetoothName = bluetoothName;
}
public void setBluetoothAddress(String bluetoothAddress) {
this.bluetoothAddress = bluetoothAddress;
}
public void setBluetoothUuid(String bluetoothUuid) {
this.bluetoothUuid = bluetoothUuid;
}
public void setBluetoothMajor(String bluetoothMajor) {
this.bluetoothMajor = bluetoothMajor;
}
public void setBluetoothMinor(String bluetoothMinor) {
this.bluetoothMinor = bluetoothMinor;
}
public void setRssi(int rssi) {
this.rssi = rssi;
}
public void setDistance(float distance) {
this.distance = distance;
}
}
BluetoothInfo.kt
data class BluetoothInfo
@JvmOverloads constructor(private val bluetoothName: String = INVALID_BT_STRING, private val bluetoothAddress: String = INVALID_BT_STRING,
private val bluetoothUuid: String = INVALID_BT_STRING, private val bluetoothMajor: String = INVALID_BT_STRING,
private val bluetoothMinor: String = INVALID_BT_STRING, private val rssi: Int = INVALID_RSSI,
private val distance: Float = INVALID_DIST) {
companion object {
const val INVALID_RSSI = 99
const val INVALID_DIST = -1f
const val INVALID_BT_STRING = "invalid"
}
override fun toString(): String {
return "name: $bluetoothName, addr: $bluetoothAddress, uuid: $bluetoothUuid \n" +
"major: $bluetoothMajor, minorL $bluetoothMinor, rssi: $rssi" +
"dist: $distance"
}
}
@Entity(tableName = "marker_table", indices = [Index(value = ["light_id"], unique = true)])
class MarkerEntity(lightId: Int, x: Int, y: Int, title: String?, description: String?, bluetoothInfo: BluetoothInfo?) {
@PrimaryKey(autoGenerate = true)
var id = 0
@ColumnInfo(name = "light_id")
var lightId: Int
@ColumnInfo(name = "lat")
var x: Int
@ColumnInfo(name = "lng")
var y: Int
@ColumnInfo(name = "title")
var title = "Default LightMarker"
@ColumnInfo(name = "description")
var description = "No Description"
@Embedded
var bluetoothInfo: BluetoothInfo? = null
override fun toString(): String {
return lightId.toString() + " " + x + " " + y + " " + title + " " + description + "\n" + bluetoothInfo.toString()
}
init {
var bluetoothInfo = bluetoothInfo
this.lightId = lightId
this.x = x
this.y = y
if (title != null) this.title = title
if (description != null) this.description = description
if (bluetoothInfo != null) {
this.bluetoothInfo = bluetoothInfo
} else {
bluetoothInfo = BluetoothInfo()
}
}
}
编辑: 我尝试了用于MarkerEntity.java的Android Studio Kotlin转换器,但错误仍然存在 MarkerEntity.kt
data class BluetoothInfo
@JvmOverloads constructor(private val bluetoothName: String = INVALID_BT_STRING, private val bluetoothAddress: String = INVALID_BT_STRING,
private val bluetoothUuid: String = INVALID_BT_STRING, private val bluetoothMajor: String = INVALID_BT_STRING,
private val bluetoothMinor: String = INVALID_BT_STRING, private val rssi: Int = INVALID_RSSI,
private val distance: Float = INVALID_DIST) {
companion object {
const val INVALID_RSSI = 99
const val INVALID_DIST = -1f
const val INVALID_BT_STRING = "invalid"
}
override fun toString(): String {
return "name: $bluetoothName, addr: $bluetoothAddress, uuid: $bluetoothUuid \n" +
"major: $bluetoothMajor, minorL $bluetoothMinor, rssi: $rssi" +
"dist: $distance"
}
}
@Entity(tableName = "marker_table", indices = [Index(value = ["light_id"], unique = true)])
class MarkerEntity(lightId: Int, x: Int, y: Int, title: String?, description: String?, bluetoothInfo: BluetoothInfo?) {
@PrimaryKey(autoGenerate = true)
var id = 0
@ColumnInfo(name = "light_id")
var lightId: Int
@ColumnInfo(name = "lat")
var x: Int
@ColumnInfo(name = "lng")
var y: Int
@ColumnInfo(name = "title")
var title = "Default LightMarker"
@ColumnInfo(name = "description")
var description = "No Description"
@Embedded
var bluetoothInfo: BluetoothInfo? = null
override fun toString(): String {
return lightId.toString() + " " + x + " " + y + " " + title + " " + description + "\n" + bluetoothInfo.toString()
}
init {
var bluetoothInfo = bluetoothInfo
this.lightId = lightId
this.x = x
this.y = y
if (title != null) this.title = title
if (description != null) this.description = description
if (bluetoothInfo != null) {
this.bluetoothInfo = bluetoothInfo
} else {
bluetoothInfo = BluetoothInfo()
}
}
}
我将BluetoothInfo.kt重写到java中,并在每个变量前面加上@ColumnInfo,以某种方式解决了这个问题 BluetoothInfo.java
@Entity(tableName = "marker_table", indices = {@Index(value = "light_id", unique = true)})
public class MarkerEntity {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "light_id")
private int lightId;
@ColumnInfo(name = "lat")
private int x;
@ColumnInfo(name = "lng")
private int y;
@ColumnInfo(name = "title")
private String title = "Default LightMarker";
@ColumnInfo(name = "description")
private String description = "No Description";
@Embedded
private BluetoothInfo bluetoothInfo;
public MarkerEntity(int lightId, int x, int y, String title, String description, BluetoothInfo bluetoothInfo) {
this.lightId = lightId;
this.x = x;
this.y = y;
if (title != null) this.title = title;
if (description != null) this.description = description;
if (bluetoothInfo != null) {
this.bluetoothInfo = bluetoothInfo;
} else {
bluetoothInfo = new BluetoothInfo();
}
}
public int getId() {
return id;
}
public int getLightId() {
return lightId;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public BluetoothInfo getBluetoothInfo() {
return bluetoothInfo;
}
public void setLightId(int light_id) {
this.lightId = light_id;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setBluetoothInfo(BluetoothInfo bluetoothInfo) {
this.bluetoothInfo = bluetoothInfo;
}
@Override
public String toString() {
return lightId + " " + x + " " + y + " " + title + " " + description + "\n" + bluetoothInfo.toString();
}
}
public class BluetoothInfo {
private static final int INVALID_RSSI = 99;
private static final float INVALID_DIST = -1f;
private static final String INVALID_BT_STRING = "invalid";
@ColumnInfo(name = "bluetooth_name")
private String bluetoothName;
@ColumnInfo(name = "bluetooth_addr")
private String bluetoothAddress;
@ColumnInfo(name = "bluetooth_uuid")
private String bluetoothUuid;
@ColumnInfo(name = "bluetooth_major")
private String bluetoothMajor;
@ColumnInfo(name = "bluetooth_minor")
private String bluetoothMinor;
@ColumnInfo(name = "rssi")
private int rssi;
@ColumnInfo(name = "distance")
private float distance;
public BluetoothInfo(String bluetoothName, String bluetoothAddress, String bluetoothUuid, String bluetoothMajor, String bluetoothMinor, int rssi, float distance){
this.bluetoothName = bluetoothName;
this.bluetoothAddress = bluetoothAddress;
this.bluetoothUuid = bluetoothUuid;
this.bluetoothMajor = bluetoothMajor;
this.bluetoothMinor = bluetoothMinor;
this.rssi = rssi;
this.distance = distance;
}
public String getBluetoothName() {
return bluetoothName;
}
public String getBluetoothAddress() {
return bluetoothAddress;
}
public String getBluetoothUuid() {
return bluetoothUuid;
}
public String getBluetoothMajor() {
return bluetoothMajor;
}
public String getBluetoothMinor() {
return bluetoothMinor;
}
public int getRssi() {
return rssi;
}
public float getDistance() {
return distance;
}
public void setBluetoothName(String bluetoothName) {
this.bluetoothName = bluetoothName;
}
public void setBluetoothAddress(String bluetoothAddress) {
this.bluetoothAddress = bluetoothAddress;
}
public void setBluetoothUuid(String bluetoothUuid) {
this.bluetoothUuid = bluetoothUuid;
}
public void setBluetoothMajor(String bluetoothMajor) {
this.bluetoothMajor = bluetoothMajor;
}
public void setBluetoothMinor(String bluetoothMinor) {
this.bluetoothMinor = bluetoothMinor;
}
public void setRssi(int rssi) {
this.rssi = rssi;
}
public void setDistance(float distance) {
this.distance = distance;
}
}
发布堆栈跟踪会很有帮助。所以,看看这是错误还是警告?。你能帮我把StackTrace贴出来吗?现在还不能复制,这问题已经解决了。顺便说一句,这是个错误。