在获取Firebase数据时出现空指针异常

时间:2018-11-01 06:00:21

标签: java android firebase firebase-realtime-database

代码

        DatabaseReference mFetchingMessages = FirebaseDatabase.getInstance().getReference().child("Messages").child(MessageSenderId).child(MessageRecieverId);


     FirebaseRecyclerOptions<MessagesHelper> options =
            new FirebaseRecyclerOptions.Builder<MessagesHelper>()
                    .setQuery(mFetchingMessages, MessagesHelper.class)
                    .build();

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MessagesHelper, Chat.MessagesViewHolder>(options) {

        @Override
        protected void onBindViewHolder(@NonNull final Chat.MessagesViewHolder holder, final int position, @NonNull MessagesHelper model) {
            holder.setMessage(model.getMessage());
            final String from_user = model.getFrom();
            final boolean Seen = model.isSeen();

            final String userId = getRef(position).getKey();
            DatabaseReference mTimeReference = FirebaseDatabase.getInstance().getReference().child("Messages").child(MessageSenderId).child(MessageRecieverId);
            mTimeReference.keepSynced(true);
            mTimeReference.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                        String message = postSnapshot.child("Message").getValue().toString();

                        if (from_user != null && from_user.equals(MessageSenderId)) {
                            holder.mDisplayMessage.setBackgroundColor(Color.BLUE);
                            holder.mDisplayMessage.setBackgroundColor(Color.WHITE);

                            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mDisplayMessage.getLayoutParams();
                            params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
                            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                            holder.mDisplayMessage.setLayoutParams(params);

                            if (Seen == true) {
                                holder.mDisplayMessage.setBackgroundColor(Color.CYAN);
                            } else {
                                holder.mDisplayMessage.setBackgroundColor(Color.BLUE);
                            }
                        } else {
                            holder.mDisplayMessage.setBackgroundColor(Color.GREEN);
                            holder.mDisplayMessage.setTextColor(Color.BLACK);

                            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mDisplayMessage.getLayoutParams();
                            params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                            holder.mDisplayMessage.setLayoutParams(params);
                        }

                        holder.setMessage(message);
                        linearLayoutManager.setStackFromEnd(true);

                    }

                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {
                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
        @Override
        public Chat.MessagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.custom_activity_chat, parent, false);
            return new Chat.MessagesViewHolder(view);
        }
    };
    messageList.setAdapter(firebaseRecyclerAdapter);

 public static class MessagesViewHolder extends RecyclerView.ViewHolder {
    View mView;
    TextView mDisplayMessage;
    public MessagesViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }
    public void setMessage(String message) {
        mDisplayMessage = (TextView) mView.findViewById(R.id.message_text_layout);
        mDisplayMessage.setText(String.valueOf(message));
    }
    public void setFrom(String from) {
    }
}

如果我删除循环,它将仅获取最后一条消息...像所有视图一样,仅显示最后一条消息,如果我添加循环,则在String message = postsnapshot.child(“消息”)

enter image description here

首先是消息中的发件人ID,内部是接收方ID,内部是推送ID

1 个答案:

答案 0 :(得分:0)

您可以将此行添加到代码中,以检查您是否有Message个孩子,然后将其领取。

似乎您的某些节点没有Message子节点。您可以在提取Message之前检查它。

if (postSnapshot.hasChild("Message")) {
     String message = postSnapshot.child("Message").getValue().toString();
}