2017-08-07 50 views
0

我正在應用爲我的開發團隊,我想補充的應用程序版本編輯所有登錄與火力實時數據庫的用戶,我使用的代碼訪問火力地堡實時數據庫值

appIdEndPoint = mDatabase.child("users").child(user.getUid()).child("app").child("id"); 
     appVersionEndPoint = mDatabase.child("users").child(user.getUid()).child("app").child("latest_version"); 
     uid_text.setText("User firebase ID: " + user.getUid()); 

     appIdEndPoint.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       appDetails address = dataSnapshot.getValue(appDetails.class); 
       if (address.getID().equals("")){ 
        appIdEndPoint.setValue(getRandom(1000, 9999)); 
       } 
       appid_text.setText("App ID: " + address.getID()); 
       appid_edittext.setText(address.getID()); 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       Log.d("AppID", databaseError.getMessage()); 
       Toast.makeText(AppDetailsEditor.this, "Failed to fetch data from database", Toast.LENGTH_LONG); 
       finish(); 
      } 
     }); 

     appVersionEndPoint.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       appDetails address = dataSnapshot.getValue(appDetails.class); 
       if (address.getVersion().equals("")){ 
        appVersionEndPoint.setValue("Waiting for developers input"); 
       } 
       appversion_text.setText("App version: " + address.getVersion()); 
       appversion_edittext.setText(address.getVersion()); 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       Log.d("AppVersion", databaseError.getMessage()); 
       Toast.makeText(AppDetailsEditor.this, "Failed to fetch data from database", Toast.LENGTH_LONG); 
       finish(); 
      } 
     }); 

我試圖用「 String appID = dataSnapshot.getValue()。toString();「但它產生java.lang.NullPointerException錯誤。請有人編輯我的代碼來獲取數據庫值? 這裏是 「appDetails.java」 類:

public class appDetails { 

public String ID = ""; 
public String version = ""; 

public appDetails(){ 

} 

public appDetails(String ID, String version){ 
    this.ID = ID; 
    this.version = version; 
} 

public String getID(){ 
    return ID; 
} 
public String getVersion(){ 
    return version; 
} 

}

編輯:

代碼顯示上面也產生錯誤顯示java.lang.NullPointerException

+0

添加數據庫模式 –

+0

你可以發佈結構你的json嗎? –

+0

我的databse json包含「null」。我嘗試添加一些新的孩子,但是當我點擊保存時,Firebase不會保存它 –

回答

0

因爲你的數據庫目前是空的,ValueListener.onDataChange()將一個空的快照火。你的代碼需要檢查這一點:

appIdEndPoint.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
    if (dataSnapshot.exists()) { 
     appDetails address = dataSnapshot.getValue(appDetails.class); 
     ... 

你需要在監聽,可能不存在數據的任何ValueEventListener這樣的檢查。

+0

似乎工作,但我現在有另一個問題,我需要檢查數據庫值「root/users/[uid]/app/id」,如果它是空的,我需要編寫隨機值正如你可以在我的代碼中所見 –

0

如果你的數據不會保存在你的Firbease數據庫,那麼你需要改變這樣的規則:

{ 
    "rules": { 
    ".read": true, 
    ".write": true 
    } 
} 

但暫時將您的規則改爲此,請不要這樣。

+0

我已經準備好了我的規則,但感謝 –

0

您的代碼不是無效的。首先地址可以爲空。其次,getID可以爲null。反正這是更好的:

if (address.getID() == null || "".equals(address.getID())){ 
    appIdEndPoint.setValue(getRandom(1000, 9999)); 
}