firebase检索数据不起作用 - 缺少简单的东西?

时间:2016-05-27 16:06:22

标签: java android database

我有一个"人员列表"包含一些常用识别信息的配置文件。

  1. 我能够以编程方式添加新条目而不会出现任何问题。
  2. 我能够检索并在列表中显示firebase中的条目
  3. 我遇到的问题是检索在上述列表中单击其条目的人员的个人资料。下面列出了此代码。我已经确认 thisPersonRef 包含正确的路径。据我所知,事件监听器只是被跳过/未被触发。我相信我遵循了https://www.firebase.com/docs/android/guide/retrieving-data.htmlhttps://firebase.google.com/docs/database/android/retrieve-data#attach_an_event_listener

    上的说明和示例

    我得到的错误是:

    java.lang.NullPointerException:尝试从字段中读取' java.lang.String com.bcll_tech.ptapp.Persons.Person.barCodeText'在空对象引用上

    我也尝试过使用addListenerForSinglevalueEvent以及ChildEventListener,我得到了相同的结果。这让我觉得我必须错过这个过程中的其他一步。

    任何帮助都会受到赞赏,因为我已经查看过清单并且没有看到任何相同的内容。

    感谢。

    public void onPersonSelected(String listRecordId) {     
        Log.d(logtag, "Activity: List item recordId = " + listRecordId);
        final     Firebase thisPersonRef = mPersonRef.child("barCodeId").child(listRecordId);
    
        thisPersonRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) { 
                Log.d(logtag, "PersonsListViewDetailsFragment: in listener. snapshot of " + thisPersonRef);
                Person thisPerson = snapshot.getValue(Person.class);
                Log.d(logtag, "From Get Data: Bar Code ID = " + thisPerson.barCodeText + "; Last name = " + thisPerson.lastName);               
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
    
        Log.d(logtag, "Activity: Get Person: BarCodeId = " + thisPerson.barCodeText);
    }
    

1 个答案:

答案 0 :(得分:0)

试试这个

 final Firebase thisPersonRef = mPersonRef.child("barCodeId").setValue(listRecordId);

 Person thisPerson = snapshot.getChildren().getValue(Person.class);