Java Android Room 库错误:找不到字段的 getter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51218118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:00:57  来源:igfitidea点击:

Android Room library error: Cannot find getter for field

javaandroid

提问by

I get this error but I couldn't find any solution. I tried many things but still I'm getting this code.

我收到此错误,但找不到任何解决方案。我尝试了很多东西,但仍然收到此代码。

My code is:https://justpaste.it/4di3y

我的代码是:https : //justpaste.it/4di3y

The error:

错误:

error: Cannot find getter for field. ( private Long fie_id )

错误:找不到字段的吸气剂。(私人 Long fie_id )

How I can fix it?

我该如何解决?

采纳答案by TomH

Instead of

代替

public Long getId() {
return fie_id;
}

public void setId(Long id) {
this.fie_id = id;
}

Do

public Long getFie_id() {
        return fie_id;
    }

    public void setFie_id(Long fie_id) {
        this.fie_id = fie_id;
    }

回答by EvOlaNdLuPiZ

During your programs lifecycle if the encapsulationis incorrect then confusion happens. After viewing your code it seems might be attempting to access something from an eternal class. PrimaryKey id is being called instead of the passed value setId( id ).

在您的程序生命周期中,如果封装不正确,则会发生混淆。查看您的代码后,它似乎试图从一个永恒的类中访问某些东西。正在调用 PrimaryKey id 而不是传递的值 setId( id )。

回答by Suleyman

It has to do with the naming of your PrimaryKey. The signature of your getter and setter should correspond to the name of the variable. Otherwise, Roomcan't find it.

它与您的PrimaryKey. 你的 getter 和 setter 的签名应该对应于变量的名称。不然Room找不到。

Which means that the variable idshould have getId(), and variable fie_idshould have getFie_id()as a getter, the same goes for the setters. So, either rename your PrimaryKeyor name your getters and setters accordingly.

这意味着变量id应该有getId(),并且变量fie_id应该有getFie_id()一个 getter,setter 也是如此。因此,请相应地重命名PrimaryKey或命名您的 getter 和 setter。

回答by Sam

It is better to stick with documantation. The structures are sensitive. link of documantationSample :

最好坚持使用文档。结构是敏感的。文档示例链接

@Entity(tableName = "notes")
public class Note {

@PrimaryKey(autoGenerate = true)
public int id;   

@NotNull
@ColumnInfo(name = "note")
public  String mNote;

public Note(@NotNull String mNote) {
    this.mNote = mNote;
}

public int getId() {
    return id;
}

@NotNull
public String getNote() {
    return this.mNote;
}}

Had same problem Clearand Rebuildthe project options are the answer somehow after the change.

有同样的问题ClearRebuild项目选项是更改后不知何故的答案。

回答by abderazak la3ta3ta

for getter in setter, just follow the Pattern get/set[A-Z]last lenght-1 of the field just like in the example ( field: mWord)

对于 setter 中的 getter,只需按照示例中的字段 get/set[AZ]last lenght-1 模式(字段:mWord)

@Entity(tableName = "words")

public class Word {

公共课词{

@PrimaryKey
@NonNull
@ColumnInfo(name = "word")
private String mWord;

public Word(@NonNull String mWord) {
    this.mWord = mWord;
}

public String getMWord() {
    return mWord;
}

}

}

回答by Sathish Gadde

Only need to change is fied scope update as "public" instead of "private".

只需要更改将域更新为“公共”而不是“私有”。

https://github.com/rzwitserloot/lombok/issues/1403

https://github.com/rzwitserloot/lombok/issues/1403

回答by Blaze Gawlik

For me it was because of an uncapitalized "m". The below works:

对我来说,这是因为没有大写的“m”。下面的作品:

@ColumnInfo(name = "foo")
private String mFoo;

public String getMFoo() {
    return mFoo;
}

public void setMFoo(String foo) {
    this.mFoo = foo;
}

This doesn't:

这不会:

@ColumnInfo(name = "foo")
private String mFoo;

public String getmFoo() {
    return mFoo;
}

public void setmFoo(String foo) {
    this.mFoo = foo;
}

Also don't forget to specify the new column in your migration. Something like:

另外不要忘记在迁移中指定新列。就像是:

val MIGRATION_2_3: Migration = object : Migration(2, 3) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE myDataObj "
                    + " ADD COLUMN foo TEXT")
        }
    }

回答by Robiul Hossain Shah Imran

Change all the access modifier to "public". This will solve the issue.

将所有访问修饰符更改为“public”。这将解决问题。

Instead of:

代替:

@PrimaryKey
private Long fie_id;

@ColumnInfo(name = "name")

private String name;

Use:

用:

@PrimaryKey
public Long fie_id;

@ColumnInfo(name = "name")

public String name;

回答by Abhishek Meharia

Try to match the name of the setter function as the variable name declared. Forex. There is a boolean variable declared like

private boolean attachment_status;

尝试将 setter 函数的名称与声明的变量名称相匹配。外汇。有一个像private boolean attach_status一样声明的布尔变量

It's getter should be like this

public boolean getattachment_status(){
return attachment_status
}

它的 getter 应该是这样的

public boolean getattachment_status(){
return attachment_status
}

or u can do like this

public boolean getAttachment_status(){
return attachment_status
}

或者你可以这样做

public boolean getAttachment_status(){
return attachment_status
}

I declared my getter like this and I was getting the same error

public boolean getAttachment(){
return attachment_status
}

我像这样声明了我的 getter 并且我收到了同样的错误

public boolean getAttachment(){
return attachment_status
}

but I refactor it and the problem was solved.

但我重构了它,问题就解决了。

回答by Boadu Philip Asare

Changing the variable from private to protected worked for me.

将变量从私有更改为受保护对我有用。