我使用Firebase创建了一个登录注册系统,然后在个人资料页面中,我从Firebase中检索了用户的数据。接下来,我为用户创建了一个编辑个人资料页面,用户可以在其中编辑信息和个人资料图片,但这是我的问题
当用户单击“保存”按钮时,将从Firebase中删除该特定用户的整个节点。这是我的代码
编辑profile.java
public class ProfileEditInfo extends AppCompatActivity {
private EditText newUserName, newUserEmail, newUserPhone;
private Button save;
private DatabaseReference databaseReference;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private ImageView updateProfilePic;
private static int PICK_IMAGE = 123;
Uri imagePath;
private StorageReference storageReference;
private FirebaseStorage firebaseStorage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_edit_info);
updateProfilePic = findViewById(R.id.ivProfileUpdate);
newUserName = findViewById(R.id.etNameUpdate);
newUserEmail = findViewById(R.id.etEmailUpdate);
newUserPhone = findViewById(R.id.etPhoneUpdate);
save = findViewById(R.id.btnSave);
databaseReference= FirebaseDatabase.getInstance().getReference("users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
String userID = firebaseAuth.getUid();
firebaseStorage = FirebaseStorage.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
final StorageReference storageReference = firebaseStorage.getReference();
storageReference.child("images").child(firebaseAuth.getUid() + "." + "jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).fit().centerCrop().into(updateProfilePic);
}
});
// final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users").child(userID);
databaseReference.child(userID).addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Users userProfile = dataSnapshot.getValue(Users.class);
newUserName.setText(userProfile.getName());
newUserPhone.setText(userProfile.getPhone());
newUserEmail.setText(userProfile.getEmail());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ProfileEditInfo.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = newUserName.getText().toString();
String phone = newUserPhone.getText().toString();
String email = newUserEmail.getText().toString();
// Users userProfile = new Users(name, phone, email);
// databaseReference.setValue(userProfile);
String id = firebaseAuth.getCurrentUser().getUid();
Users user = new Users(id,name, email, phone);
databaseReference.child(id).setValue(user);
StorageReference imageReference = storageReference.child("images").child(firebaseAuth.getUid() + "." + "jpg"); //User id/Images/Profile Pic.jpg
UploadTask uploadTask = imageReference.putFile(imagePath);
uploadTask.addOnFailureListener(new OnFailureListener() {
public void onFailure(@NonNull Exception e) {
Toast.makeText(ProfileEditInfo.this, "Upload failed!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ProfileEditInfo.this, DisplayProfile.class));
finish();
}
}).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
Toast.makeText(ProfileEditInfo.this, "Upload successful!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ProfileEditInfo.this, DisplayProfile.class));
finish();
}
});
finish();
}
});
updateProfilePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data.getData() != null) {
imagePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagePath);
updateProfilePic.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
users.java的代码在这里
Users.java
package com.example.deepak.myapplication;
public class Users {
public String imageUrl;
public String imageName;
public String uid;
public String name;
public String email;
public String password;
public String phone;
public Users(){}
public Users(String id, String name, String phone, String email){}
public Users(String uid, String name, String email, String password, String phone) {
this.uid = uid;
this.name = name;
this.email = email;
this.password = password;
this.phone = phone;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
数据库结构在这里
数据库结构
当我单击编辑信息时,我能够在编辑文本框中检索所有数据,但是一旦编辑数据并单击“保存”按钮,将删除我创建用户所使用的用户ID的整个节点,然后得到空指针例外
所以请帮助e,让我知道我要去哪里了 谢谢。
答案 0 :(得分:1)
Firebase要求每个节点至少有一个具有键和值的孩子。 两者。只有一个没有价值的钥匙作为孩子,或者根本没有孩子,这将导致整个节点被删除。我建议您先确保已将用户对象设置为值,然后再将其保存到Firebase。
PS:我在Android上使用StackExchange应用程序,无法找到添加评论的方法,否则wud hav将其添加为一个。有人面对吗?