【发布时间】:2017-07-30 14:25:31
【问题描述】:
在加载 FirebaseUser 信息的 showdata 方法中,我的 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;
}
}
【问题讨论】:
-
你找到答案了吗?
-
不,没有,我尝试了答案 1,但它不起作用,给出相同的错误并且 uInfo 仍然具有空值,我使用 User_03 登录,userId 是正确的 Id,但 ds 有 User_01 key 和 values ,然后 ds.getValue(UserInformations.class).getName() == User_01 name
-
我已经在下面发布了我的答案。
标签: android firebase firebase-realtime-database nullpointerexception