在showdata方法中加载FirebaseUser信息我对uInfo有空值,我运行应用程序并用User_02登录,调试显示: ds.getChild(userId)到达登录并加载了右键的权限用户,但是当尝试从UserInformations.class获取值时,我给出了NullPointerException,那么该怎么回事呢?
我不是说ds具有User_01的键和值。那是正常的吗?
public class AddToDatabase extends AppCompatActivity {
private static final String TAG = "AddToDatabase";
private ListView userInfolist ;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
String userId;
UserInformations uInfo =new UserInformations();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_database);
userInfolist = (ListView) findViewById(R.id.userInfolist);
mAuth = FirebaseAuth.getInstance();
FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference myRef = mFirebaseDatabase.getReference();
FirebaseUser user = mAuth.getCurrentUser();
userId = user.getUid();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
toastMassege(" You are successfly signed in with " + user.getEmail());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
toastMassege(" You are successfly signed out ");
}
}
};
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
private void showData(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
uInfo.setName(ds.child(userId).getValue(UserInformations.class).getName()); // set the name
uInfo.setEmail(ds.child(userId).getValue(UserInformations.class).getEmail()); // set the Email
uInfo.setPhone_num(ds.child(userId).getValue(UserInformations.class).getPhone_num()); // set the PhonNum
ArrayList<String> arrayList =new ArrayList<>();
arrayList.add(uInfo.getName());
arrayList.add(uInfo.getEmail());
arrayList.add(uInfo.getPhone_num());
ArrayAdapter<String> adapter =new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrayList);
userInfolist.setAdapter(adapter);
}
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
private void toastMassege(String massege) {
Toast.makeText(this, massege, Toast.LENGTH_SHORT).show();
}
}
UserInformation.java
class UserInformations {
private String Email,Name,Phone_num ;
UserInformations() {
}
String getEmail() {
return Email;
}
void setEmail(String email) {
this.Email = email;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
String getPhone_num() {
return Phone_num;
}
void setPhone_num(String phone_num) {
this.Phone_num = phone_num;
}
}
答案 0 :(得分:1)
首先,将UserInformations
中的变量从私有更改为公开。
class UserInformations {
public String Email,Name,Phone_num ;
}
其次,您必须将Firebase路径从Phone number
更改为Phone_num
。为什么?好吧,那是因为你在String Phone_num
内宣布UserInformation
。
您当前的JSON:
{
"uid001": {
"Email": "user01@gmail.com",
"Name": "user01",
"Phone number": "0123456789"
},
"uid002": {
"Email": "user02@gmail.com",
"Name": "user02",
"Phone number": "0123456789"
},
}
更改为JSON以下:
{
"uid001": {
"Email": "user01@gmail.com",
"Name": "user01",
"Phone_num": "0123456789"
},
"uid002": {
"Email": "user02@gmail.com",
"Name": "user02",
"Phone_num": "0123456789"
},
}
答案 1 :(得分:0)
当您使用for(DataSnapshot ds : dataSnapshot.getChildren())
时,ds
已经拥有作为用户节点的子节点的数据,而不需要child(userId)
直接执行此操作:
uInfo.setName(ds.child("name").getValue());
uInfo.setEmail(ds.child("email").getValue());
uInfo.setPhone_num(ds.child("phone_num").getValue());