Android Firebase valueEventListener导致应用崩溃

时间:2018-04-13 11:09:22

标签: android firebase listview firebase-realtime-database

应用说明

我的应用程序从Firebase数据库中检索数据。首先,我正在将java对象正确地推送到数据库,并且可以使用片段onActivityCreated中的addListenerForSingleValueEvent方法无任何问题地检索数据。总共我有4个片段,使用ViewPager实现。其中三个片段各包含一个ListView,每个片段中都显示不同类型的数据。下载数据并将其添加到每个片段的Lis​​tView中。当用户单击ListView项时,将显示包含和编辑文本的对话框。用户输入一个值并单击“保存”。用户输入的值将被推送到数据库,其中包含其他参数,例如date,jobID等作为另一个Java对象(Status.class)当我推送这个对象时,我使用的是DatabaseReference.CompletionListener,而在onComplete方法中我正在执行一些操作。其中一个操作是重新创建Fragment的Parent活动,以允许根据项目操作状态更新UI。我没想到每次ListView中的一个项目被执行时重新创建父活动是非常有效的。所以我决定使用ValueEventListener而不是SingleValueEventListener,它应该更新该项的ListView。 ValueEventListener可以很好地创建ListView,但是当用户第一次单击任何ListView项目,并且他们输入一个值并单击保存时,应用程序随后崩溃并出现以下错误。当应用程序重新启动时,ListView中的项目现在以不同的方式显示,因为它已被操作。单击任何项​​目并保存从那时起的数据工作正常。仅当用户第一次保存数据时,应用程序才会崩溃。我不知道是什么导致了它。

错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
   at android.view.LayoutInflater.from(LayoutInflater.java:233)
   at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:181)
   at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:166)
   at com.project.nolan.john.application.IOAdapter.<init>(IOAdapter.java:0)
   at com.project.nolan.john.application.Fragment_Sensors.makeListViewFromDataSnapshot(Fragment_Sensors.java:178)
   at com.project.nolan.john.application.Fragment_Sensors.access$000(Fragment_Sensors.java:58)
   at com.project.nolan.john.application.Fragment_Sensors$1.onDataChange(Fragment_Sensors.java:141)
   at com.google.android.gms.internal.zzegf.zza(Unknown Source)
   at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
   at com.google.android.gms.internal.zzeig.run(Unknown Source)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6682)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)*

片段中的值事件侦听器onActivityCreated

 databaseReference.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            mPointsList = new ArrayList<>();

            for (DataSnapshot ds : dataSnapshot.getChildren())
            {
                IO_Point io = ds.getValue(IO_Point.class);

                if(io != null)
                {
                    if(io.getSiteUID().equals(mSiteUID) && io.getType().equals("S"))
                    {
                        mPointsList.add(io);
                    }
                }
            }

            if(mSensors_IOPointList != null)
            {
                mListAdapter = new IOAdapter(getActivity(), mPointsList);
            }

            mListView.setAdapter(mListAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError)
        {

        }
    });

如果您需要更多信息/代码,请告诉我,我将编辑我的问题。

修改

IO_Point.class

    public class IO_Point
{
    private String type;
    private String lan;
    private String os;
    private String ionum;
    private String label;
    private String siteUID;
    private Boolean actioned;
    private String iouid;

    public IO_Point()
    {

    }

    public IO_Point(String type, String lan, String os, String ioNum, String label, String siteUID, Boolean actioned, String iouid)
    {
        this.type = type;
        this.lan = lan;
        this.os = os;
        this.ionum = ioNum;
        this.label = label;
        this.siteUID = siteUID;
        this.actioned = actioned;
        this.iouid = iouid;

    }

    public String getType()
    {
        return type;
    }

    public String getLan()
    {
        return lan;
    }

    public String getOS()
    {
        return os;
    }

    public String getIONum()
    {
        return ionum;
    }

    public String getLabel()
    {
        return label;
    }

    public String getSiteUID()
    {
        return siteUID;
    }

    public Boolean getActioned()
    {
        return actioned;
    }

    public String getIOUID()
    {
        return iouid;
    }

}

数据库样本

    {
  "IO Points" : {
    "-L9fzerDpRXR72ilvRht" : {
      "actioned" : true,
      "ionum" : "S1",
      "iouid" : "-L9fzerDpRXR72ilvRht",
      "label" : "Sensor One",
      "lan" : "1",
      "os" : "11",
      "siteUID" : "-L9fz_QYz8KFQtByYOxN",
      "type" : "S"
    },
    "-L9fzij0Kyt1x3F_W7Ea" : {
      "actioned" : false,
      "ionum" : "I1",
      "iouid" : "-L9fzij0Kyt1x3F_W7Ea",
      "label" : "Digital One",
      "lan" : "1",
      "os" : "11",
      "siteUID" : "-L9fz_QYz8KFQtByYOxN",
      "type" : "D"
    },
    "-L9fzlCaDhagrIzIzTTI" : {
      "actioned" : false,
      "ionum" : "D1",
      "iouid" : "-L9fzlCaDhagrIzIzTTI",
      "label" : "Output One",
      "lan" : "1",
      "os" : "11",
      "siteUID" : "-L9fz_QYz8KFQtByYOxN",
      "type" : "O"
    },
    "-L9g-Ppg9noyYepYRLMq" : {
      "actioned" : false,
      "ionum" : "S1",
      "iouid" : "-L9g-Ppg9noyYepYRLMq",
      "label" : "Sensor Two",
      "lan" : "1",
      "os" : "11",
      "siteUID" : "-L9g-Hr-v8zgO08zWGTP",
      "type" : "S"
    },
    "-L9g-XOoOSlv15PKiC0e" : {
      "actioned" : false,
      "ionum" : "I1",
      "iouid" : "-L9g-XOoOSlv15PKiC0e",
      "label" : "Digital Two",
      "lan" : "1",
      "os" : "11",
      "siteUID" : "-L9g-Hr-v8zgO08zWGTP",
      "type" : "D"
    },
    "-L9g-k0poRsnDi5ZyHjP" : {
      "actioned" : false,
      "ionum" : "D1",
      "iouid" : "-L9g-k0poRsnDi5ZyHjP",
      "label" : "Output Two",
      "lan" : "1",
      "os" : "11",
      "siteUID" : "-L9g-Hr-v8zgO08zWGTP",
      "type" : "O"
    }
  },
  "Sites" : {
    "-L9fz_QYz8KFQtByYOxN" : {
      "inProgress" : true,
      "siteAddress" : "Site One Address",
      "siteName" : "Site One",
      "siteUID" : "-L9fz_QYz8KFQtByYOxN"
    },
    "-L9g-Hr-v8zgO08zWGTP" : {
      "inProgress" : false,
      "siteAddress" : "Site Two Address",
      "siteName" : "Site Two",
      "siteUID" : "-L9g-Hr-v8zgO08zWGTP"
    }
  }
}

2 个答案:

答案 0 :(得分:1)

&#34;尝试在空对象引用上调用虚方法&#34;意味着你试图用它做某事的这个对象没有被初始化。调试你的程序,你必须找到你松散的地方&#34;这个 。检查&#34; LayoutInflater.java:233&#34; !!

中的对象

答案 1 :(得分:1)

您已移除Firebase自定义子键并使用默认

DatabaseReference save = FirebaseDatabase.getInstance().getReference("users");
String uid = save.push().getKey();
save.child(uid).setValue(new user(uid,"Milan"));