简体   繁体   English

没有找到 Android Firebase 的设置器/字段

[英]No setter/field for found Android Firebase

I used FirebaseRecyclerAdapter to get all the childs of "Pro" using a Model class named " Spacecraft" and now I want to retrieve all the candidates into a child of Pro like "1"我使用 FirebaseRecyclerAdapter 使用名为“Spacecraft”的模型类获取“Pro”的所有子项,现在我想将所有候选项检索到 Pro 的子项中,例如“1”

I created a public static "candidat" into "Spacecraft" and I used the setters and getters but still the same error我在“航天器”中创建了一个公共静态“候选对象”,并使用了 setter 和 getter,但仍然出现相同的错误

This is my database:这是我的数据库:

数据库

this is the Model Class这是模型类

public class Spacecraft{
    private String name;
    private String desc;
    private String last;
    private candidat candidat;

    public Spacecraft.candidat getCandidat() {
        return candidat;
    }

    public void setCandidat(Spacecraft.candidat candidat) {
        this.candidat = candidat;
    }

    public Spacecraft() {
    }
    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last = last;
    }
    public static class candidat{

        private String info;
        private String namecandid;
        public candidat(){}
        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }

        public String getNamecandid() {
            return namecandid;
        }

        public void setNamecandid(String namecandid) {
            this.namecandid = namecandid;
        }
    }
}

This is my code for FirebaseRecyclerAdapter这是我的 FirebaseRecyclerAdapter 代码

@Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Spacecraft, candidatviewholder> firebaseRecyclerAdapter  = new FirebaseRecyclerAdapter<Spacecraft, candidatviewholder>(
                Spacecraft.class,
                R.layout.candidat,
                candidatviewholder.class,
                query){
                        @Override
                        protected void populateViewHolder(candidatviewholder viewHolder, Spacecraft model, int position) {
                            viewHolder.setName1(model.getCandidat().getNamecandid());
                            viewHolder.setInfo1(model.getCandidat().getInfo());
                        }
                    };
        rv.setAdapter(firebaseRecyclerAdapter);
    }

The error:错误:

No setter/field for key1 found on class com.example.ilyas.evotingapplication.Spacecraft$candidat在类 com.example.ilyas.evotingapplication.Spacecraft$candidat 上找不到 key1 的设置器/字段

I had this error but the above solutions didn't fix it.我有这个错误,但上述解决方案没有解决它。 Hopefully, this alternate solution will help others.希望这个替代解决方案能帮助其他人。 If you have that error occur for almost every variable, chances are that you have Proguard enabled and it is removing the un-used getter and setter methods.如果几乎每个变量都发生该错误,则可能是您启用了 Proguard,并且它正在删除未使用的 getter 和 setter 方法。 To fix this, add a line similar to this to your proguard-rules.pro file:要解决此问题,请在您的 proguard-rules.pro 文件中添加与此类似的行:

-keep class com.example.yourapp.ObjectClass

where ObjectClass is the name of your java object class that is stored to Firebase.其中 ObjectClass 是存储到 Firebase 的 Java 对象类的名称。

I think it's just that your data models on Firebase and in Java differ.我认为这只是您在 Firebase 和 Java 上的数据模型不同。

In your java class, the Spacecraft class has a candidat field of type Candidat .在您的 java 类中, Spacecraft类有一个Candidat类型的candidat字段。 But, in the database, the candidat field is really a nested object (map), containing one key Key1 , which value is a Candidat structure.但是,在数据库中, candidat字段实际上是一个嵌套对象(映射),包含一个键Key1 ,该值是一个Candidat结构。

So, depending on what did you want to achieve:因此,取决于您想要实现的目标:

  • if you wanted each spacecraft to have exactly one candidat: save the database object properly, so {info: "info 1", namecandid: "name 1"} is saved directly under candidat field, not one level deeper, so the field has type Candidat in the code.如果您希望每个航天器都只有一个候选对象:正确保存数据库对象,因此{info: "info 1", namecandid: "name 1"}直接保存在candidat字段下,而不是更深一层,因此该字段具有类型代码中的Candidat
  • if you wanted each spacecraft to have a few candidats: instead of private Candidat candidat field, it should be typed Map<String, Candidat> , because that's the type it has in your database screenshot.如果您希望每个航天器都有一些候选对象:而不是private Candidat candidat字段,它应该键入Map<String, Candidat> ,因为这是它在您的数据库屏幕截图中的类型。

Work for me: - keepclassmembers class com.myPackageName.MyClassName { *;为我工作: - keepclassmembers class com.myPackageName.MyClassName { *; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [CustomClassMapper]:在 Firebase Firestore 的 class 上找不到 ... 的设置器/字段 - [CustomClassMapper]: No setter/field for … found on class in Firebase Firestore 如何解决Android Firebase没有设置器/字段的问题? - How to solve Android Firebase No setter/field Problem? Firebase,classmaper:在类“ myclass”上找不到“ UserTwo”的设置器/字段 - Firebase , classmaper : no setter/field found for 'UserTwo' on class 'myclass' firebase 实时数据库 W/ClassMapper:在课堂上找不到 setter/field - firebase realtime database W/ClassMapper: No setter/field for found on class 未找到二传手/字段 - No setter/field found 关于Android Firebase检索数据(无设置者/字段错误) - About Android Firebase retrieve data(No setter/field error) 重复的:在课堂上找不到二传手 - Duplicated : No setter/field for found on class 在 class 上找不到 cardimage 的设置器/字段 - No setter/field for cardimage found on class Firebase数据库未检索数据W / ClassMapper:在类上找不到-KeAi52QSaiuf7p5jEYM的设置器/字段 - Firebase database not retrieving data W/ClassMapper: No setter/field for -KeAi52QSaiuf7p5jEYM found on class Android Firebase addOnChildListener致命异常“打印请求消息”并且没有设置器/字段 - Android Firebase addOnChildListener Fatal Exception “print-in needs message” and No setter/field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM