Android 存储引用获取不正确,无法解决
大家好,我有一个问题,我已经试着解决了三天没有停止,但我不能解决它,这是绝望的,这是与存储参考 当我调用Android 存储引用获取不正确,无法解决,android,firebase,firebase-storage,picasso,android-glide,Android,Firebase,Firebase Storage,Picasso,Android Glide,大家好,我有一个问题,我已经试着解决了三天没有停止,但我不能解决它,这是绝望的,这是与存储参考 当我调用getReference()方法时,它会给我一个“位置不能为空或null”,但问题是如果我正在提供存储引用,并且当尝试使用getReferenceFromUrl()时,运行,你可以使用Glide或picasso获得照片,但要返回到我的活动,我会得到一个null指针异常,我不能用它。这是吗 "java.lang.NullPointerException: Attempt to invoke v
getReference()
方法时,它会给我一个“位置不能为空或null”,但问题是如果我正在提供存储引用,并且当尝试使用getReferenceFromUrl()
时,运行,你可以使用Glide或picasso获得照片,但要返回到我的活动,我会得到一个null指针异常,我不能用它。这是吗
"java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.net.Uri.getLastPathSegment ()' on a null object
reference"
我用这种方法发送的照片很成功,但当我想把它们保存起来,用毕加索或glide的IMageView时,我就遇到了这个问题
我尝试了所有可能的方法,但都不起作用
这门课给了我很多问题。异常问题出现在StorageReference行的populateViewHolder中,我将文件放在sendComment
方法中
private void initCommentSection() {
RecyclerView commentRecyclerView = (RecyclerView) findViewById(R.id.comment_recyclerview);
commentRecyclerView.setLayoutManager(new LinearLayoutManager(CommentActivity.this));
在populateVIewHolder中,getLastPathSegment()的存储引用中存在问题
我找到了解决办法
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);
}
if(mselectedimageri!=null){
mComent.setUser(用户);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
FirebaseUtils.getCommentsImagesRef().child(mSelectedImageUri.getLastPathSegment()).putFile(mSelectedImageUri).addOnSuccessListener(CommentActivity.this,new OnSuccessListener()){
@凌驾
成功时公共无效(UploadTask.TaskSnapshot TaskSnapshot){
字符串url=Constants.COMMENT_IMAGES+“/”+mselectedimageri.getLastPathSegment();
setPhotoCommentUrl(url);
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.设定值(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS\u KEY,uid);
}
});
}否则{
mComent.setUser(用户);
mComent.setCommentId(uid);
mComent.setComment(strComment);
mComent.setTimeCreated(System.currentTimeMillis());
字符串urlnull=Constants.PHOTO\u COMMENT\u ROW+“/”+“bite.png”;
mComent.setPhotoCommentUrl(urlnull);
FirebaseUtils.getCommentRef(mPost.getPostId())
.child(uid)
.设定值(mComent);
FirebaseUtils.getMyCommentRef().child(uid).setValue(true);
FirebaseUtils.addToMyRecord(Constants.COMMENTS\u KEY,uid);
}
这里的代码太多,无法有效地提供帮助。请发布(阅读链接,它非常有用)。一旦你可以用一个方法重现问题,只需要硬编码的值,就可以更容易地看出问题所在。@FrankvanPuffelen现在我只是编辑了它,并将必要的可能的副本放入其中
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);
}