No setter/field for found Android Firebase

吃可爱长大的小学妹 提交于 2021-02-08 08:42:12

问题


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"

I created a public static "candidat" into "Spacecraft" and I used the setters and getters but still the same error

This is my database:

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

@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


回答1:


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. To fix this, add a line similar to this to your proguard-rules.pro file:

-keep class com.example.yourapp.ObjectClass

where ObjectClass is the name of your java object class that is stored to Firebase.




回答2:


I think it's just that your data models on Firebase and in Java differ.

In your java class, the Spacecraft class has a candidat field of type Candidat. But, in the database, the candidat field is really a nested object (map), containing one key Key1, which value is a Candidat structure.

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.
  • 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.



回答3:


Work for me: -keepclassmembers class com.myPackageName.MyClassName { *; }



来源:https://stackoverflow.com/questions/41650103/no-setter-field-for-found-android-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!