W/ClassMapper: No setter/field for class

2020-05-29 06:47发布

问题:

I'm simply just trying to populate data from Firebase Database into my recyclerview. My recyclerview shows perfectly, but the adapter won't set the values to the text in recyclerview textview, All it says is " No setter/field for -L57t4-97c3dLZjA_yXC found on class com.example.barry.starcity.StreetClass". Which makes me think that I didn't have my setters made correctly, but there was auto-generated by Android Studio. I don't know what I am missing here. Any help is appreciated.

NODE OBJECT

  class StreetClass {

private String id;
private String semail;
private String sname;
private String stype;
private String sdetail;
private String slocation;
private String sdate;
private String imgurl;

public StreetClass(){

}

public String getImgurl() {
    return imgurl;
}

public void setImgurl(String imgurl) {
    this.imgurl = imgurl;
}

public String getId() {return id;}

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

public String getEmail() {
    return semail;
}

public void setEmail(String email) {
    this.semail = email;
}

public String getName() {
    return sname;
}

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

public String getType() {
    return stype;
}

public void setType(String type) {
    this.stype = type;
}

public String getDetail() {
    return sdetail;
}

public void setDetail(String detail) {
    this.sdetail = detail;
}

public String getLocation() {
    return slocation;
}

public void setLocation(String location) {
    this.slocation = location;
}

public String getDate() {
    return sdate;
}

public void setDate(String date) {
    this.sdate = date;
}
}

RecyclerViewAdapter

 public class RecyclerViewAdapter extends 
 RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;
List<StreetClass>listdata ;

public RecyclerViewAdapter(Context context, List<StreetClass> list) {

    this.listdata = list;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    StreetClass AllDetails = listdata.get(position);

    holder.NameTextView.setText(AllDetails.getName());
    holder.DetailTextView.setText(AllDetails.getDetail());
    holder.EmailTextView.setText(AllDetails.getEmail());
    holder.DateTextView.setText(AllDetails.getDate());
    holder.LocationTextView.setText(AllDetails.getLocation());
    holder.TypeTextView.setText(AllDetails.getType());
}

@Override
public int getItemCount() {

    return listdata.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    public TextView NameTextView;
    public TextView DetailTextView;
    public TextView EmailTextView;
    public TextView DateTextView;
    public TextView LocationTextView;
    public TextView TypeTextView;
    /*public ImageView ImageTextView;*/

    public ViewHolder(View itemView) {

        super(itemView);
        NameTextView = itemView.findViewById(R.id.ShowNameTextView);
        DetailTextView = itemView.findViewById(R.id.ShowDetailTextView);
        EmailTextView = itemView.findViewById(R.id.ShowEmailTextView);
        DateTextView = itemView.findViewById(R.id.ShowDateTextView);
        LocationTextView = itemView.findViewById(R.id.ShowLocationTextView);
        TypeTextView = itemView.findViewById(R.id.ShowTypeTextView)

    }
  }
 }

MainActivity

  public class StatusFragment extends Fragment {

DatabaseReference databaseStatus;
ProgressDialog progressDialog;
List<StreetClass> list = new ArrayList<StreetClass>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;

public StatusFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_status, container, false);

    recyclerView = rootView.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext().getApplicationContext()));

    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Loading Data from Firebase Database");
    progressDialog.show();

    databaseStatus = FirebaseDatabase.getInstance().getReference().child("Street Problems");
    databaseStatus.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {

                StreetClass streetClass = dataSnapshot.getValue(StreetClass.class);

                list.add(streetClass);

            }
            adapter = new RecyclerViewAdapter(getContext().getApplicationContext(), list);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            progressDialog.dismiss();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });

    return  rootView;
}

And the layout of an item:

    <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="5dp"
    card_view:contentPadding="5dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardMaxElevation="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ECEFF1"
        android:padding="10dp">

        <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Student Name: "
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/Detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Detail: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Name"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/Email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Email: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Detail"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Date: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Email"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/Location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Location "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Date"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

      <!--  <TextView
            android:id="@+id/Image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Type"
            android:layout_toStartOf="@+id/ShowNameTextView"
            android:text="Uploaded image: "
            android:textColor="#000"
            android:textSize="10dp" />-->

        <TextView
            android:id="@+id/Type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Type: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Location"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />



        <TextView
            android:id="@+id/ShowNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Show Student Name"
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/Name"
            android:layout_toEndOf="@+id/Name"
            android:layout_marginLeft="19dp"
            android:layout_marginStart="19dp" />


        <TextView
            android:id="@+id/ShowDetailTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowNameTextView"
            android:layout_below="@+id/ShowNameTextView"
            android:gravity="center"
            android:text="Show Detail"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowEmailTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowNameTextView"
            android:layout_below="@+id/ShowDetailTextView"
            android:gravity="center"
            android:text="Show Email"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowDateTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowEmailTextView"
            android:layout_below="@+id/ShowEmailTextView"
            android:gravity="center"
            android:text="Show Date"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowLocationTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowDateTextView"
            android:layout_below="@+id/ShowDateTextView"
            android:gravity="center"
            android:text="Show Location"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowTypeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowDateTextView"
            android:layout_below="@+id/ShowLocationTextView"
            android:gravity="center"
            android:text="Show Type"
            android:textColor="#000"
            android:textSize="10dp" />

      <!--  <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ShowImageView"
            android:layout_width="90dp"
            android:layout_height="50dp"
            android:layout_alignStart="@+id/ShowTypeTextView"
            android:layout_below="@+id/ShowTypeTextView" />-->



    </RelativeLayout>
</android.support.v7.widget.CardView>

This is my database structure

    java.lang.NullPointerException: Attempt to invoke virtual method 'void 
    android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a 
    null object reference

This is my Log cat

see this is my new database

In my App it looks like this

Logcat

回答1:

Even if I don't see your database structure, I can say from your code that your asking for everything under Street Problems, which includes all of the objects you pushed previously. I can see from the error message that it's trying to find a setter or field for the push ID -L57t4-97c3dLZjA_yXC that it found just under Street Problems node. If you want to get a single object, you're going to have to dig into the objects in the push IDs under Street Problems. Your code should look something like this:

dataSnapshot.child("Street Problems/-L57t4-97c3dLZjA_yXC").getValue(StreetClass.class);

If you want all StreetClass objects, then simply loop through its childrens like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Street Problems");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                StreetClass streetClass = dSnapshot.getValue(StreetClass.class);
                Log.d("TAG", streetClass.getName());
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);

This is an example on how to display the names using getName() method.

Edit:

As i see on your updatedquestion, the warning is because the casing mismatches between your field and you setter.

You have a field named and the corresponding setters and getters which are not correct!

private String sname;

public String getName() {
    return sname;
}

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

The correct setters and getters should be:

public String getSname() {
    return sname;
}

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

The same problems is also with the other fields.

This is the correct way of structuring your model class:

public class StreetClass {
    private String id;
    private String semail;
    private String sname;
    private String stype;
    private String sdetail;
    private String slocation;
    private String sdate;
    private String imgurl;

    public StreetClass(){}

    public StreetClass(String id, String semail, String sname, String stype, String sdetail, String slocation, String sdate, String imgurl) {
        this.id = id;
        this.semail = semail;
        this.sname = sname;
        this.stype = stype;
        this.sdetail = sdetail;
        this.slocation = slocation;
        this.sdate = sdate;
        this.imgurl = imgurl;
    }

    public String getId() {
        return id;
    }

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

    public String getSemail() {
        return semail;
    }

    public void setSemail(String semail) {
        this.semail = semail;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getStype() {
        return stype;
    }

    public void setStype(String stype) {
        this.stype = stype;
    }

    public String getSdetail() {
        return sdetail;
    }

    public void setSdetail(String sdetail) {
        this.sdetail = sdetail;
    }

    public String getSlocation() {
        return slocation;
    }

    public void setSlocation(String slocation) {
        this.slocation = slocation;
    }

    public String getSdate() {
        return sdate;
    }

    public void setSdate(String sdate) {
        this.sdate = sdate;
    }

    public String getImgurl() {
        return imgurl;
    }

    public void setImgurl(String imgurl) {
        this.imgurl = imgurl;
    }
}

You can also use the String class as below:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Street Problems");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                String sname = dSnapshot.child("sname").getValue(String.class);
                Log.d("TAG", sname);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);