我仍在使用我的登录应用程序。目前,用户可以注册,登录和更新个人资料信息。数据存储在Firebase实时数据库中,并且也可以通过Firebase运行。我想添加“删除配置文件”按钮,以便从Firebase中删除用户信息。我想出了如何从强化过程中删除用户。现在,我尝试对Uid调用remove()函数,因此现在数据已在实时数据库中删除,但仍在Firebase身份验证中,如果删除用户数据,应用程序将崩溃。因此,只有删除过程之一可以工作...强化或从数据库中删除用户。 也许问题在于实时数据库中的结构和子/父关系?我的建议是onClickListener()中的顺序可能不正确? 我如何一次将它们都删除并使其正常工作? 提前谢谢:)
public class UpdateProfilActivity extends AppCompatActivity {
private EditText newUserVorname, newUserNachname,newUserStrasse,newUserHnr,newUserPlz,newUserStadt,newUserLand;
private Button speichern;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_profil);
newUserVorname=findViewById(R.id.editTextVornameUpdate);
newUserNachname=findViewById(R.id.editTextNachnameUpdate);
newUserStrasse=findViewById(R.id.editTextStrasse);
newUserHnr=findViewById(R.id.editTextHNr);
newUserPlz=findViewById(R.id.editTextPlz);
newUserStadt=findViewById(R.id.editTextStadt);
newUserLand=findViewById(R.id.editTextLand);
speichern=findViewById(R.id.buttonSpeichern);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
firebaseAuth= FirebaseAuth.getInstance();
firebaseDatabase= FirebaseDatabase.getInstance();
final DatabaseReference databaseReference= firebaseDatabase.getReference(firebaseAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserProfil userProfil=dataSnapshot.getValue(UserProfil.class);
newUserVorname.setText(userProfil.getVorname());
newUserNachname.setText(userProfil.getNachname());
newUserStrasse.setText(userProfil.getStrasse());
newUserHnr.setText(userProfil.getHnr());
newUserPlz.setText(userProfil.getPlz());
newUserStadt.setText(userProfil.getStadt());
newUserLand.setText(userProfil.getLand());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(UpdateProfilActivity.this,"Database Error",Toast.LENGTH_SHORT).show();
}
});
speichern.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
String Vorname = newUserVorname.getText().toString();
String Nachname = newUserNachname.getText().toString();
String Strasse = newUserStrasse.getText().toString();
String HNr = newUserHnr.getText().toString();
String Plz = newUserPlz.getText().toString();
String Stadt = newUserStadt.getText().toString();
String Land = newUserLand.getText().toString();
UserProfil userProfil=new UserProfil(Vorname,Nachname,Strasse,HNr,Plz,Stadt,Land);
databaseReference.setValue(userProfil);
finish();
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
UserProfil
public class UserProfil {
public String vorname;
public String nachname;
public String strasse;
public String hnr;
public String plz;
public String stadt;
public String land;
public UserProfil(){
}
public UserProfil(String Vorname, String Nachname,String Strasse,String HNr,String Plz,String Stadt,String Land) {
this.vorname = Vorname;
this.nachname = Nachname;
this.strasse= Strasse;
this.hnr= HNr;
this.plz=Plz;
this.stadt=Stadt;
this.land=Land;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getNachname() {
return nachname;
}
public void setNachname(String nachname) {
this.nachname = nachname;
}
public void setStrasse(String strasse) {
this.strasse = strasse;
}
public String getStrasse() {
return strasse;
}
public void setHnr(String hnr) {
this.hnr = hnr;
}
public String getHnr() {
return hnr;
}
public void setPlz(String plz) {
this.plz = plz;
}
public String getPlz() {
return plz;
}
public void setStadt(String stadt) {
this.stadt = stadt;
}
public String getStadt() {
return stadt;
}
public void setLand(String land) {
this.land = land;
}
public String getLand() {
return land;
}
这是崩溃的日志
Process: com.example.login, PID: 29022
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.ProfileActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in FirebaseDatabase.getReference()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in FirebaseDatabase.getReference()
at com.google.firebase.database.FirebaseDatabase.getReference(com.google.firebase:firebase-database@@19.0.0:164)
at com.example.login.ProfileActivity.onCreate(ProfileActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
编辑:
问题出现在以下行中: DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());
public class ProfileActivity extends AppCompatActivity {
private TextView profilVorname,profilNachname,profilStrasse,profilHNr,profilPlz,profilStadt,profilLand;
private Button profilUpdate,PasswortUpdate;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
profilVorname= findViewById(R.id.textViewPVorname);
profilNachname=findViewById(R.id.textViewPNachname);
profilUpdate=findViewById(R.id.buttonProfilUpdate);
profilStrasse=findViewById(R.id.textViewPStrasse);
profilHNr=findViewById(R.id.textViewPHNr);
profilPlz=findViewById(R.id.textViewPPlz);
profilStadt=findViewById(R.id.textViewPStadt);
profilLand=findViewById(R.id.textViewPLand);
PasswortUpdate=findViewById(R.id.buttonPasswordUpdate);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
firebaseAuth=FirebaseAuth.getInstance();
firebaseDatabase= FirebaseDatabase.getInstance();
DatabaseReference databaseReference= firebaseDatabase.getReference(firebaseAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserProfil userProfil=dataSnapshot.getValue(UserProfil.class);
profilVorname.setText(userProfil.getVorname());
profilNachname.setText(userProfil.getNachname());
profilStrasse.setText(userProfil.getStrasse());
profilHNr.setText(userProfil.getHnr());
profilPlz.setText(userProfil.getPlz());
profilStadt.setText(userProfil.getStadt());
profilLand.setText(userProfil.getLand());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this,"Database Error",Toast.LENGTH_SHORT).show();
}
});
profilUpdate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
startActivity(new Intent(ProfileActivity.this,UpdateProfilActivity.class));
}
});
PasswortUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ProfileActivity.this,UpdatePasswortActivity.class));
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(ProfileActivity.this, NavActivity.class));
}
答案 0 :(得分:0)
您遇到以下错误:
原因:java.lang.NullPointerException:无法为FirebaseDatabase.getReference()中的参数“ pathString”传递null。
因为您在下面的代码行中将getReference()
的值传递给null
方法:
DatabaseReference databaseReference= firebaseDatabase.getReference(firebaseAuth.getUid());
因此firebaseAuth.getUid()
返回null
是因为firebaseAuth
对象是null
,这意味着您的用户未通过身份验证。
要解决此问题,首先请确保您的用户已通过身份验证。其次,要实际获得uid
,您需要在getUid()
对象上调用FirebaseUser
,而在FirebaseAuth
对象上调用 not 。因此,请将上面的代码行更改为:
String uid = firebaseAuth.getCurrentUser().getUid();
// ^ ^
DatabaseReference databaseReference= firebaseDatabase.getReference(uid);