大家好,我有一个问题,我试图解决3天没有停止,但我无法解决它,这是绝望的,它是与StorageReference,
当我调用getReference()
方法时,它给了我一个“位置不能为空或空”但关键是如果我给出存储引用,并且当尝试使用getReferenceFromUrl()
时,运行,你可以用Glide或者用毕加索,但要返回我的活动,我得到一个空指针异常,我不能使用它。这是
"java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.net.Uri.getLastPathSegment ()' on a null object
reference"
我在方法发送中的照片成功到达但是当想要拯救他们在带有毕加索或滑行的IMageView中使用它们时会给我这个问题
我尝试了所有可能的方法,但它不起作用
这是给我带来问题的课程。异常问题是在StorageRefence行的populate viewHolder中,我把文件放在sendComment
方法中
private void initCommentSection() {
RecyclerView commentRecyclerView = (RecyclerView) findViewById(R.id.comment_recyclerview);
commentRecyclerView.setLayoutManager(new LinearLayoutManager(CommentActivity.this));
这里的populateVIewHolder是存储引用中的问题,在getLastPathSegment()中
FirebaseRecyclerAdapter<Comment, CommentHolder> commentAdapter =
new FirebaseRecyclerAdapter<Comment, CommentHolder>(
Comment.class,
R.layout.row_comment,
CommentHolder.class,
FirebaseUtils.getCommentRef(mPost.getPostId())) {
@Override
protected void populateViewHolder(final CommentHolder viewHolder, Comment model, int position) {
viewHolder.setUsername(mUser.getUsername());
viewHolder.setComment(model.getComment());
viewHolder.setTime(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
StorageReference sdsd = FirebaseStorage.getInstance().getReferenceFromUrl(FirebaseUtils.getCommentsImagesRef().child(mSelectedImageUri.getLastPathSegment()).toString());
sdsd.getDownloadUrl().addOnSuccessListener(CommentActivity.this, new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(CommentActivity.this).load(uri).into(viewHolder.imageBetaComm);
}
});
if(mSelectedImageUri == null){
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/memetics-e9fac.appspot.com/o/bite_carpet%2Fbite.png?alt=media&token=36b90aed-5448-4493-836a-ad5554848820");
storageReference.getDownloadUrl().addOnSuccessListener(CommentActivity.this, new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.with(CommentActivity.this).load(uri).into(viewHolder.imageBetaComm);
}
});
}
}
};
commentRecyclerView.setAdapter(commentAdapter);
}
ans在这里将照片发送到存储
private void sendComment() {
final ProgressDialog progressDialog = new ProgressDialog(CommentActivity.this);
progressDialog.setMessage("Sending comment..");
progressDialog.setCancelable(true);
progressDialog.setIndeterminate(true);
progressDialog.show();
final String uid = FirebaseUtils.getUid();
final String strComment = mCommentEditTextView.getText().toString();
FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".", ","))
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if(mSelectedImageUri != null){
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
FirebaseUtils.getCommentsImagesRef().child(mSelectedImageUri.getLastPathSegment()).putFile(mSelectedImageUri);
}else{
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
}
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.setValue(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY, uid);
FirebaseUtils.getPostRef().child(mPost.getPostId())
.child("numComments")
.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
long num = (long) mutableData.getValue();
mutableData.setValue(num + 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
progressDialog.dismiss();
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY,uid);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
我找到了解决方案
if(mSelectedImageUri != null){
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
FirebaseUtils.getCommentsImagesRef().child(mSelectedImageUri.getLastPathSegment()).putFile(mSelectedImageUri).addOnSuccessListener(CommentActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = Constants.COMMENT_IMAGES + "/" + mSelectedImageUri.getLastPathSegment();
mComent.setPhotoCommentUrl(url);
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.setValue(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY, uid);
}
});
}else{
mComent.setUser(user);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
String urlnull = Constants.PHOTO_COMMENT_ROW + "/" + "bite.png";
mComent.setPhotoCommentUrl(urlnull);
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.setValue(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS_KEY, uid);
}