奇怪的错误java.lang.IllegalArgumentException:location不能为null或空Firebase Android

时间:2017-03-27 20:26:39

标签: android firebase firebase-realtime-database

你好,所有出现的异常都是奇怪的,我不知道为什么它给了我真相,而且很好地工作了#34; post"他们可以在没有任何问题的情况下上传图片现在给了我那种错误,我不知道如何解决它,因为我还是新手

例外:

 FATAL EXCEPTION: main
                                                                       Process: com.android.octa.memetixs, PID: 15679
                                                                       Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
                                                                       java.lang.IllegalArgumentException: location must not be null or empty
                                                                           at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
                                                                           at com.google.firebase.storage.FirebaseStorage.getReference(Unknown Source)
                                                                           at com.android.octa.memetixs.ui.fragment.PostFragment$2.populateViewHolder(PostFragment.java:102)
                                                                           at com.android.octa.memetixs.ui.fragment.PostFragment$2.populateViewHolder(PostFragment.java:84)
                                                                           at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:196)
                                                                           at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
                                                                           at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                           at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                           at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                           at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                           at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                           at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                           at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
                                                                           at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735)
                                                                           at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1579)
                                                                           at android.widget.LinearLayout.onLayout(LinearLayout.java:1488)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                           at android.view.View.layout(View.java:16639)
                                                                           at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735)
                                                                        at android.widget.Line

错误的片段:

public class PostFragment extends Fragment {

private View mRootView;
private FirebaseRecyclerAdapter<Post, PostHolder> mPostAdapter;
private RecyclerView mPostRecyclerView;
private User mmUser;
private Post mmPost;


public PostFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mRootView = inflater.inflate(R.layout.fragment_post, container, false);


    FloatingActionButton fab = (FloatingActionButton) mRootView.findViewById(R.id.fabM);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PostCreateDialog dialog = new PostCreateDialog();
            dialog.show(getFragmentManager(), null);
        }
    });

     init();
    return mRootView;
}

private void init() {
    mPostRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerview_post);
    mPostRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    setupAdapter();
    mPostRecyclerView.setAdapter(mPostAdapter);
}

private void setupAdapter() {

    mPostAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(
            Post.class,
            R.layout.row_post,
            PostHolder.class,
            FirebaseUtils.getPostRef()
    ) {
        @Override
        protected void populateViewHolder(PostHolder viewHolder, final Post model, int position) {
            mmUser = new User();
            mmPost = new Post();
            viewHolder.setNumComments(String.valueOf(model.getNumComments()));
            viewHolder.setNumLikes(String.valueOf(model.getNumLikes()));
            viewHolder.setTime(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
            viewHolder.setUsername(mmUser.getUser());
            viewHolder.setPostText(model.getPostText());







            StorageReference storageReference = FirebaseStorage.getInstance()
                    .getReference(mmPost.getPostImageUrl());
            Glide.with(getActivity())
                    .using(new FirebaseImageLoader())
                    .load(storageReference)
                    .into(viewHolder.postDisplayImageView);

            viewHolder.postLikeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onLikeClick(model.getPostId());
                }
            });

            viewHolder.postCommentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), CommentActivity.class);
                    intent.putExtra(Constants.EXTRA_POST, model);
                    startActivity(intent);
                }
            });


        }
    };

}

private void onLikeClick(final String postId) {
    FirebaseUtils.getPostLikedRef(postId)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() != null) {
                        //user liked
                        FirebaseUtils.getPostRef()
                                .child(postId)
                                .child("numLikes")
                                .runTransaction(new Transaction.Handler() {
                                    @Override
                                    public Transaction.Result doTransaction(MutableData mutableData) {
                                        long num = mutableData.getValue(Long.class);
                                        mutableData.setValue(num - 1);
                                        return Transaction.success(mutableData);
                                    }

                                    @Override
                                    public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
                                        FirebaseUtils.getPostLikedRef(postId)
                                                .setValue(null);
                                    }
                                });
                    } else {
                        FirebaseUtils.getPostRef()
                                .child(postId)
                                .child("numLikes")
                                .runTransaction(new Transaction.Handler() {
                                    @Override
                                    public Transaction.Result doTransaction(MutableData mutableData) {
                                        long num = mutableData.getValue(Long.class);
                                        mutableData.setValue(num + 1);
                                        return Transaction.success(mutableData);
                                    }

                                    @Override
                                    public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
                                        FirebaseUtils.getPostLikedRef(postId)
                                                .setValue(true);
                                    }
                                });
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}


public static class PostHolder extends RecyclerView.ViewHolder {


    TextView postOwnerUsernameTextView;
    TextView postTimeCreatedTextView;

    ImageView postDisplayImageView;
    TextView postTextTextView;

    LinearLayout postLikeLayout;
    TextView postNumLikesTextView;

    LinearLayout postCommentLayout;
    TextView postNumCommentsTextView;


    public  PostHolder(View itemView) {
        super(itemView);

        postOwnerUsernameTextView = (TextView) itemView.findViewById(R.id.tv_post_username);
        postTimeCreatedTextView = (TextView) itemView.findViewById(R.id.tv_time);
        postDisplayImageView = (ImageView) itemView.findViewById(R.id.iv_post_display);
        postLikeLayout = (LinearLayout) itemView.findViewById(R.id.like_layout);
        postCommentLayout = (LinearLayout) itemView.findViewById(R.id.comment_layout);
        postNumLikesTextView = (TextView) itemView.findViewById(R.id.tv_likes);
        postNumCommentsTextView = (TextView) itemView.findViewById(R.id.tv_comments);
        postTextTextView = (TextView) itemView.findViewById(R.id.tv_post_text);
    }

    public void setUsername(String username) {
        postOwnerUsernameTextView.setText(username);
    }

    public void setTime(CharSequence time) {
        postTimeCreatedTextView.setText(time);
    }

    public void setNumLikes(String numLikes) {
        postNumLikesTextView.setText(numLikes);
    }

    public void setNumComments(String numComments) {
        postNumCommentsTextView.setText(numComments);
    }

    public void setPostText(String text) {
        postTextTextView.setText(text);
    }

}
 }

firebase参考的实用程序:

      public class FirebaseUtils {

//==============================================================================================
// USER REFERENCE
public static DatabaseReference getUserRef(String email) {
    return FirebaseDatabase.getInstance()
            .getReference(Constants.USERS_KEYS)
            .child(email);
}
public static FirebaseUser getCurrentUser() {
    return FirebaseAuth.getInstance().getCurrentUser();
}






              // POST REFERENCE
public static DatabaseReference getPostRef() {
    return FirebaseDatabase.getInstance()
            .getReference(Constants.POSTS_KEY);
}
public static Query getPostQuery() {
    return getPostRef().orderByChild(Constants.TIME_CREATED_KEY);
}
public static DatabaseReference getMyPostRef() {
    return FirebaseDatabase.getInstance().getReference(Constants.MY_POSTS)
            .child(getCurrentUser().getEmail().replace(".", ","));
}





// POST LIKED REFERENCE
public static DatabaseReference getPostLikedRef() {
    return FirebaseDatabase.getInstance()
            .getReference(Constants.POSTS_LIKED_KEY);
}
public static DatabaseReference getPostLikedRef(String postId) {
    return getPostLikedRef().child(getCurrentUser().getEmail()
            .replace(".", ","))
            .child(postId);
}








// USER ID
public static String getUid() {
    String path = FirebaseDatabase.getInstance().getReference().push().toString();
    return path.substring(path.lastIndexOf("/") + 1);
}








// POST IMAGE REFERENCE
public static StorageReference getImagesSRef() {
    return FirebaseStorage.getInstance().getReference(Constants.POST_IMAGES);
}








// COMMENT REFERENCE
public static DatabaseReference getCommentRef(String postId) {
    return FirebaseDatabase.getInstance().getReference(Constants.COMMENTS_KEY)
            .child(postId);
}







// RECORD

public static DatabaseReference getMyRecordRef() {
    return FirebaseDatabase.getInstance().getReference(Constants.USER_RECORD)
            .child(getCurrentUser().getEmail().replace(".", ","));
}



public static void addToMyRecord(String node,final String id) {
    FirebaseUtils.getMyRecordRef().child(node).runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            ArrayList<String> myPostCollection;
            if (mutableData.getValue() == null) {
                myPostCollection = new ArrayList<>(1);
                myPostCollection.add(id);
                mutableData.setValue(myPostCollection);
            } else {
                myPostCollection = (ArrayList<String>) mutableData.getValue();
                myPostCollection.add(id);
                mutableData.setValue(myPostCollection);
            }
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {

        }
    });
}

}

和我的课程发送帖子:

 public class PostCreateDialog extends DialogFragment implements View.OnClickListener {

private static final int RC_PHOTO_PICKER = 1;
private View mRootView;
private Post mPost;

private ProgressDialog mProgressDialog;
private Uri mSelectedImageUri;
private ImageView mPostDisplay;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    mPost = new Post();

    mProgressDialog = new ProgressDialog(getContext());
    mRootView = getActivity().getLayoutInflater().inflate(R.layout.post_create_dialog, null);
    mPostDisplay = (ImageView) mRootView.findViewById(R.id.post_dialog_display);
    mRootView.findViewById(R.id.post_dialog_select_imageview).setOnClickListener(this);
    mRootView.findViewById(R.id.post_dialog_send_imageview).setOnClickListener(this);
    builder.setView(mRootView);
    return builder.create();


}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.post_dialog_send_imageview:
            sendPost();
            break;
        case R.id.post_dialog_select_imageview:
            selectImage();
            break;
    }
}


private void selectImage(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/jpeg");
    intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}

private void sendPost(){
    mProgressDialog.setMessage("Sending post...");
    mProgressDialog.setCancelable(false);
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.show();

    FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".", ","))
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    final String postUid = FirebaseUtils.getUid();
                    TextView postDialogTextView = (TextView) mRootView.findViewById(R.id.post_dialog_edittext);
                    String text = postDialogTextView.getText().toString();

                    mPost.setUser(user);
                    mPost.setNumComments(0);
                    mPost.setNumLikes(0);
                    mPost.setTimeCreated(System.currentTimeMillis());
                    mPost.setPostId(postUid);
                    mPost.setPostText(text);

                    if (mSelectedImageUri != null) {
                        FirebaseUtils.getImagesSRef()
                                .child(mSelectedImageUri.getLastPathSegment())
                                .putFile(mSelectedImageUri)
                                .addOnSuccessListener(getActivity(),
                                        new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                            @Override
                                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                                String url = Constants.POST_IMAGES + "/" + mSelectedImageUri.getLastPathSegment();
                                                //String downloadUrl = taskSnapshot.getDownloadUrl().toString();
                                                mPost.setPostImageUrl(url);
                                                addToMyPostList(postUid);
                                            }


                                        });
                    } else {
                        addToMyPostList(postUid);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    mProgressDialog.dismiss();
                }
            });
}

private void addToMyPostList(final String postUid){
    FirebaseUtils.getPostRef().child(postUid)
            .setValue(mPost);
    FirebaseUtils.getMyPostRef().child(postUid).setValue(true)
            .addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    mProgressDialog.dismiss();
                    dismiss();
                }
            });

    FirebaseUtils.addToMyRecord(Constants.POSTS_KEY, postUid);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_PHOTO_PICKER) {
        if (resultCode == RESULT_OK) {
            mSelectedImageUri = data.getData();
            mPostDisplay.setImageURI(mSelectedImageUri);
        }
    }

}
}

1 个答案:

答案 0 :(得分:1)

我认为你的问题在这里:

  //You only initialize. Never assign a value to PostImageUrl()
  mmPost = new Post();
            viewHolder.setNumComments(String.valueOf(model.getNumComments()));
            viewHolder.setNumLikes(String.valueOf(model.getNumLikes()));
            viewHolder.setTime(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
            viewHolder.setUsername(mmUser.getUser());
            viewHolder.setPostText(model.getPostText());







            StorageReference storageReference = FirebaseStorage.getInstance()
                     //This reference location is empty. mmPost is only initialized, and doesn't have PostURL assigned to it yet.
                    .getReference(mmPost.getPostImageUrl());
            Glide.with(getActivity())

您需要在创建帖子对象后通过指定值来获取对帖子URL的引用:

mmPost = new Post();
mmPost.setPostURL(FirebaseUtils.getMyPostReference());

或类似的东西。