我是 Android 开发新手,仍在学习基础知识并尝试获得不同的访问权限我的数据库中有此结构我正在获取状态值并使用 at 作为用户类型
我收到了这个错误
java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.gab.quadrantms.LoginActivity$1$1$1.onDataChange(LoginActivity.java:116)
登录
private DatabaseReference mUserType;
mUserType = FirebaseDatabase.getInstance().getReference().child("Users").child("status");
mUserType.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// THIS IS WHERE THE ERROR IS POINTING
String status = dataSnapshot.child("status").getValue().toString();
if(status.equals("Project Manager"))
{
Intent intent = new Intent(LoginActivity.this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else
{
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
最佳答案
FirebaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(current_user_id);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String status = dataSnapshot.child("status").getValue().toString();
if(status.equals("Project Manager"))
{
Intent intent = new Intent(LoginActivity.this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else
{
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
首先,您需要创建对节点Users
的引用,然后因为您需要访问子status
,并且由于您在节点下有一个随机ID >用户
。您必须在 dataSnapshot 的直接子级(在本例中为节点 Users
)内循环,您将能够获取 status
的值。
关于java - Android - Firebase 访问级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136035/