Android studio firebase auth uid 在数据库中使用
Android studio firebase auth uid using in database
我正在尝试通过 android studio 构建应用程序。我想使用 Firebase auth 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.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.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.
用户个人资料classactivity:
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 时,我会得到上面的错误。
数据库结构
您需要迭代获取数据:
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);
}
}
我正在尝试通过 android studio 构建应用程序。我想使用 Firebase auth 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.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.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.
用户个人资料classactivity:
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 时,我会得到上面的错误。
数据库结构
您需要迭代获取数据:
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);
}
}