我试图通过android studio构建应用。我想使用Firebase身份验证uid显示用户配置文件。但是我很致命:
FATAL EXCEPTION: main
Process: al.ff.boutiqueshopping, PID: 2728
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at al.ff.boutiqueshopping.UserProfile$1.onDataChange(UserProfile.java:47)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.3.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.3.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.3.0:55)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Application terminated.
User Profile类活动:
public class UserProfile extends AppCompatActivity {
FirebaseAuth auth;
DatabaseReference reff;
TextView a,b,c,d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
auth = FirebaseAuth.getInstance();
a=findViewById(R.id.fullnameTxt);
b=findViewById(R.id.fulladdresTxt);
c=findViewById(R.id.fullphonTxt);
d=findViewById(R.id.fullemailText);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String uid = user.getUid();
Toast.makeText(UserProfile.this, uid,
Toast.LENGTH_SHORT).show();
reff= FirebaseDatabase.getInstance().getReference("user/"+uid);
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("username").getValue().toString();
String add = dataSnapshot.child("userphone").getValue().toString();
String phon = dataSnapshot.child("useraddress").getValue().toString();
String e = dataSnapshot.child("useremail").getValue().toString();
a.setText(name);
b.setText(add);
d.setText(phon);
c.setText(e);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
}
}
}
在获取用户uid并在Toast中显示时,我没有任何问题。但是当我在数据库中使用uid时,我会得到上面的错误。
数据库结构
答案 0 :(得分:1)
您需要进行迭代以获取数据:
reff= FirebaseDatabase.getInstance().getReference("user/"+uid);
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String name = ds.child("username").getValue().toString();
String add = ds.child("userphone").getValue().toString();
String phon = ds.child("useraddress").getValue().toString();
String e = ds.child("useremail").getValue().toString();
a.setText(name);
b.setText(add);
d.setText(phon);
c.setText(e);
}
}