Android Studio Firebase数据库返回null对象引用

时间:2017-07-06 17:54:45

标签: android firebase firebase-realtime-database

Hello Guys我需要你我有Firebase实时数据库的问题我把数据成功地放到了Firebase但是当我尝试从Firebase检索它时我遇到了一个问题我是如何放置数据的

private void user_info(String user_id, String user_display_name) {

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);

    HashMap<String, String> userMap = new HashMap<>();
    userMap.put("Name",user_display_name);
    userMap.put("Balls","30");
    userMap.put("Level","1");
    mProgressDialog.setMessage("Please Wait...");

    mDatabase.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

            if (task.isSuccessful()){

            }
            else
                Toast.makeText(WelcomeActivity.this, ""+task.getException(), Toast.LENGTH_SHORT).show();
                mProgressDialog.dismiss();
        }
    });

}

以及我如何尝试检索数据

private FirebaseUser mUser;
private DatabaseReference mDatabase;
private String level,level1;
private int lev,i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level_acticity);

    //Image
    mImg_level1 = (ImageView)findViewById(R.id.mImg_lev1);
    mImg_level2 = (ImageView)findViewById(R.id.mImg_lev2);
    mImg_level3 = (ImageView)findViewById(R.id.mImg_lev3);
    mImg_level4 = (ImageView)findViewById(R.id.mImg_lev4);
    mImg_level5 = (ImageView)findViewById(R.id.mImg_lev5);
    mImg_level6 = (ImageView)findViewById(R.id.mImg_lev6);
    mImg_level7 = (ImageView)findViewById(R.id.mImg_lev7);
    mImg_level8 = (ImageView)findViewById(R.id.mImg_lev8);
    mImg_level9 = (ImageView)findViewById(R.id.mImg_lev9);
    mImg_level10 = (ImageView)findViewById(R.id.mImg_lev10);
    mImg_level11 = (ImageView)findViewById(R.id.mImg_lev11);




    //Firebase
    mUser = FirebaseAuth.getInstance().getCurrentUser();
    String user_id = mUser.getUid();



    mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            level = dataSnapshot.child("Level").getValue().toString();
            if (level.equals("1")){
                level1 = "1";
            }



            }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    Toast.makeText(this, level1, Toast.LENGTH_SHORT).show();

toast没有显示任何内容,因为它为null

1 个答案:

答案 0 :(得分:1)

更改: -

mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            level = dataSnapshot.child("Level").getValue().toString();
            if (level.equals("1")){
                level1 = "1";
            }



            }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    Toast.makeText(this, level1, Toast.LENGTH_SHORT).show();

到此: -

mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        level = dataSnapshot.child("Level").getValue().toString();
        if (level.equals("1")){
            level1 = "1";

        }
         Toast.makeText(this, level1, Toast.LENGTH_SHORT).show();
        }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

这可行的原因是Firebase异步下载并且您的代码行同步执行。