我正在應用爲我的開發團隊,我想補充的應用程序版本編輯所有登錄與火力實時數據庫的用戶,我使用的代碼訪問火力地堡實時數據庫值
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
添加數據庫模式 –
你可以發佈結構你的json嗎? –
我的databse json包含「null」。我嘗試添加一些新的孩子,但是當我點擊保存時,Firebase不會保存它 –