Есть несколько хороших конструкторов, и Room выберет конструктор без аргументов - PullRequest
1 голос
/ 17 января 2020

У меня есть только один конструктор, но я не знаю, почему компилятор продолжает говорить «Есть несколько хороших конструкторов, и Room выберет конструктор без аргументов».

Я использую java и Kotlin одновременно с переходом на Kotlin, но не очень хорошо с ним знаком.

Встроенный класс BluetoothInfo находится в Kotlin, а класс MarkerEntity в java.

MarkerEntity. 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();
    }
}

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"
    }

}


РЕДАКТИРОВАТЬ:

Я пробовал Android Studio Kotlin конвертер для MarkerEntity. java, но ошибка все еще сохраняется.

MarkerEntity.kt

@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()
        }
    }
}

1 Ответ

1 голос
/ 17 января 2020

Я как-то исправил проблему, переписав BluetoothInfo.kt в java и поместив @ColumnInfo перед каждой переменной.

BluetoothInfo. java

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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...