Android Studio无法识别setter和getter

问题描述 投票:-1回答:1

我一直在尝试使用Room从SQLite数据库中读取数据,即使在我使用了Android Studio的自动生成的getter和setter之后,我仍然不断地给出错误,因为我无法找到数据对象类的getter和setter。领域。

我也得到错误:错误:实体和Pojos必须有一个可用的公共构造函数。

这是我的代码:

package mergerobotics.memo.backend.Room;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;
import android.arch.persistence.room.Database;


@Entity(tableName = "competitions")
public class CompetitionsEntity {

    @NonNull

    @PrimaryKey
    private String mId;

    @ColumnInfo(name = "competition")
    private String mCompName;

    @ColumnInfo(name = "year")
    private String mCompYear;

    public CompetitionsEntity(String Id, String CompName, String CompYear){
        this.mCompName = CompName;
        this.mCompYear = CompYear;
        this.mId = Id;
    }

    @NonNull
    public String getmId() {
        return mId;
    }

    public void setmId(@NonNull String mId) {
        this.mId = mId;
    }

    public String getmCompName() {
        return mCompName;
    }

    public void setmCompName(String mCompName) {
        this.mCompName = mCompName;
    }

    public String getmCompYear() {
        return mCompYear;
    }

    public void setmCompYear(String mCompYear) {
        this.mCompYear = mCompYear;
    }

}
android android-room setter getter
1个回答
3
投票

更改构造函数参数中的变量名称以匹配上面类中声明的变量名称。您的构造函数应如下所示:

public CompetitionsEntity(String mId, String mCompName, String mCompYear){
        this.mCompName = mCompName;
        this.mCompYear = mCompYear;
        this.mId = mId;
    }`
© www.soinside.com 2019 - 2024. All rights reserved.