【发布时间】:2017-09-04 23:23:29
【问题描述】:
大家好,我有一个问题,我尝试了 3 天没有停止,但我无法解决,这很绝望,它与 StorageReference 相关,
当我调用 getReference() 方法时,它给了我一个“位置不能为空或 null”,但关键是如果我给出了存储引用,并且在尝试与 getReferenceFromUrl() 一起使用时,运行,你可以使用 Glide 或 picasso 获取照片,但要返回我的活动,我得到一个空指针异常,我无法使用它。这是吗
"java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.net.Uri.getLastPathSegment ()' on a null object
reference"
我在发送方法中放入的照片成功到达,但是当想要拯救它们以在 IMageView 中使用它们与 picasso 或 glide 时,我遇到了这个问题
我尝试了所有可能的方法,但它不起作用
这是给我带来问题的课程。异常问题出现在 StorageRefence 行中的填充 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();
}
});
}
【问题讨论】:
-
这里的代码太多,无法有效提供帮助。请张贴minimal code that is needed to reproduce the problem(阅读链接,它非常有用)。一旦您可以通过一种方法(仅使用硬编码值)重现问题,就可以更轻松地查看问题所在。
-
@FrankvanPuffelen 现在我只是对其进行了编辑并添加了必要的内容
标签: android firebase firebase-storage picasso android-glide