gpt4 book ai didi

java - Firebase 数据库 : App Crashing When New Node is Created

转载 作者:行者123 更新时间:2023-12-02 11:31:53 25 4
gpt4 key购买 nike

我的家庭 Activity 有一个监听器附加到特定于给定用户 ID 的 Firebase 数据库用户节点。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_home, null, false);
drawer.addView(contentView, 0);

mAuth = FirebaseAuth.getInstance();
myRef = FirebaseDatabase.getInstance().getReference();
final FirebaseUser currentUser = mAuth.getCurrentUser();

fnEditText = (EditText) findViewById(R.id.fnEditTextHome);
......
ratingBar = (RatingBar) findViewById(R.id.ratingBar);

final String userID = currentUser.getUid();

ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot,userID);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};

myRef.addValueEventListener(postListener);
}

private void showData(DataSnapshot dataSnapshot, String userID) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
UserInformation uInfo = new UserInformation();
uInfo.setFirstName(ds.child(userID).getValue(UserInformation.class).getFirstName()); //set the name
uInfo.setLastName(ds.child(userID).getValue(UserInformation.class).getLastName()); //set the name
uInfo.setAddress(ds.child(userID).getValue(UserInformation.class).getAddress()); //set the name
uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail()); //set the email
uInfo.setPhoneNo(ds.child(userID).getValue(UserInformation.class).getPhoneNo()); //set the phone_num
uInfo.setRating(ds.child(userID).getValue(UserInformation.class).getRating()); //set the rating

fnEditText.setText(uInfo.getFirstName());
lnEditText.setText(uInfo.getLastName());
phoneEditText.setText(uInfo.getPhoneNo());
addressEditText.setText(uInfo.getAddress());
String d = uInfo.getRating();
Float rating = Float.parseFloat(d);
ratingBar.setRating(rating);
//display all the information
Log.d(TAG, "showData: name: " + uInfo.getFirstName());
Log.d(TAG, "showData: email: " + uInfo.getEmail());
}
}

上面的代码没有任何错误。当我在 Firebase DB 控制台上更改值时,它成功更新到 EditText。

This is how the database looks.

现在,在发布 Activity 中,我有此代码。

private void executeUploadTask(){
Toast.makeText(PostActivity.this, "uploading image", Toast.LENGTH_SHORT).show();

final String postId = FirebaseDatabase.getInstance().getReference().push().getKey();

final StorageReference storageReference = FirebaseStorage.getInstance().getReference()
.child("posts/users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() +
"/" + postId + "/post_image");

UploadTask uploadTask = storageReference.putBytes(mUploadBytes);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(PostActivity.this, "Post Success", Toast.LENGTH_SHORT).show();

//insert the download url into the firebase database
@SuppressWarnings("VisibleForTests")
Uri firebaseUri = taskSnapshot.getDownloadUrl();

Log.d(TAG, "onSuccess: firebase download url: " + firebaseUri.toString());
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

Post post = new Post();
post.setImage(firebaseUri.toString());
post.setContact_email(mContactEmail.getText().toString());
post.setContact_number(mContactNumber.getText().toString());
post.setDescription(mDescription.getText().toString());
post.setPost_id(postId);
post.setPrice(mPrice.getText().toString());
post.setTitle(mTitle.getText().toString());
post.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());

reference.child(getString(R.string.node_posts))
.child(postId)
.setValue(post);

resetFields();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(PostActivity.this, "could not upload photo", Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
@SuppressWarnings("VisibleForTests") double currentProgress = (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
if( currentProgress > (mProgress + 15)){
mProgress = currentProgress;
Log.d(TAG, "onProgress: upload is " + mProgress + "& done");
Toast.makeText(PostActivity.this, mProgress + "%", Toast.LENGTH_SHORT).show();
}
}
});
}

This is now how the database looks在新节点上发布项目后,如果我返回第一个 Activity ,它现在会崩溃。删除帖子节点可以阻止应用程序崩溃。

这就是崩溃发生的地方。

error1

error2

最佳答案

要解决此问题,请更改这行代码:

myRef = FirebaseDatabase.getInstance().getReference();

myRef = FirebaseDatabase.getInstance().getReference().child("users");

请同时更改这行代码:

UserInformation uInfo = new UserInformation();

UserInformation uInfo = ds.getValue(UserInformation.class);

并注释所有这些代码行:

uInfo.setFirstName(ds.child(userID).getValue(UserInformation.class).getFirstName()); //set the name
uInfo.setLastName(ds.child(userID).getValue(UserInformation.class).getLastName()); //set the name
uInfo.setAddress(ds.child(userID).getValue(UserInformation.class).getAddress()); //set the name
uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail()); //set the email
uInfo.setPhoneNo(ds.child(userID).getValue(UserInformation.class).getPhoneNo()); //set the phone_num
uInfo.setRating(ds.child(userID).getValue(UserInformation.class).getRating()); //set the rating

您不需要创建新对象,您已经从数据库中获取了它。

关于java - Firebase 数据库 : App Crashing When New Node is Created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49236451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com