下面的代码假设根据我的userlvlid更改我的数据库引用,但每当我运行包含此部分的活动时,应用程序崩溃我不明白为什么我不能再更改“ref”了。
ref = FirebaseDatabase.getInstance().getReference("users");
ref.child(uid).child("userlvlid").addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userlvlid = dataSnapshot.getValue().toString();
if(userlvlid.compareTo("1")==0){
Toast.makeText(MainScreen.this,userlvlid,
Toast.LENGTH_LONG).show();
ref = FirebaseDatabase.getInstance().getReference("bystander");
}
else if(userlvlid.compareTo("2")==0){
Toast.makeText(MainScreen.this,userlvlid,
Toast.LENGTH_LONG).show();
ref = FirebaseDatabase.getInstance().getReference("firstaider");
}
else if(userlvlid.compareTo("3")==0){
Toast.makeText(MainScreen.this,userlvlid,
Toast.LENGTH_LONG).show();
ref = FirebaseDatabase.getInstance().getReference("ambulance");
}
else{
//do nothing
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
}
);