我正在从Firebase拍摄数据,但无法将其添加到列表中

时间:2018-11-03 14:37:55

标签: java android firebase firebase-realtime-database

我实际上是获取数据并打印出来的,但是当我添加到列表中时,它给出了空对象异常。我该如何解决该问题呢?由于创建一个空对象而出现错误。我不明白为什么。该怎么办?

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()){
                Mac mac = new Mac();
                mac.setMackodu(ds.getValue(Mac.class).getMackodu());
                mac.setTakimlar(ds.getValue(Mac.class).getTakimlar());
                mac.setTahmin(ds.getValue(Mac.class).getTahmin());
                System.out.println("macss " + mac.getMackodu());
                System.out.println("macss " + mac.getTakimlar());
                System.out.println("macss " + mac.getTahmin());
                macList.add(mac);

            }

            myRecyclerAdapter = new MyRecyclerAdapter(getContext(),macList);
            recyclerView.setAdapter(myRecyclerAdapter);
            myRecyclerAdapter.notifyDataSetChanged();

        }
public class Mac{

    private String macKodu;
    private String takimlar;
    private String tahmin;

    public Mac() {
    }

    public Mac(String macKodu, String takimlar, String tahmin) {
        this.macKodu = macKodu;
        this.takimlar = takimlar;
        this.tahmin = tahmin;
    }

    public String getMacKodu() {
        return macKodu;
    }

    public void setMacKodu(String macKodu) {
        this.macKodu = macKodu;
    }

    public String getTakimlar() {
        return takimlar;
    }

    public void setTakimlar(String takimlar) {
        this.takimlar = takimlar;
    }

    public String getTahmin() {
        return tahmin;
    }

    public void setTahmin(String tahmin) {
        this.tahmin = tahmin;
    }

1 个答案:

答案 0 :(得分:1)

放入这两行

myRecyclerAdapter = new MyRecyclerAdapter(getContext(),macList);
recyclerView.setAdapter(myRecyclerAdapter); 

出于

myRef.addValueEventListener