2016-05-27 30 views
0

我有一個包含一些常用標識信息的「人員」配置文件列表。firebase檢索數據不工作 - 缺少一些簡單的東西?

  1. 我能夠以編程方式添加新條目,沒有任何問題。
  2. 我能夠在列表中火力

我遇到的問題是與檢索其條目點擊在上述名單中的人士的個人資料檢索和顯示的條目。下面列出了這個代碼。我已驗證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); 
} 

回答

0

嘗試這樣

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

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

AKHIL,感謝您的快速反應。我嘗試了你的建議,但是,他們沒有幫助。第一個生成「不兼容類型」錯誤消息。第二個生成「無法解析方法」,getValue ...' – RDR80LSR

+0

順便說一句,我不明白爲什麼「setValue」,當我試圖「獲取價值」... – RDR80LSR

+0

你不能直接分配字符串值到firebase 。因此,在將整個類傳遞給setValue()之後,您需要定義POJO類(在定義listRecordId變量中使用單獨的類) –

相關問題