**关闭。**此题需要debugging details。目前不接受答复。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
昨天关门了。
Improve this question
我尝试使用Firebase进行聊天,当我尝试显示用户已提交的消息时,出现以下错误:
java.lang.NullPointerException:尝试在com.example.javalearn.Fragment.ChatFragment$2.onBindViewHolder(ChatFragment.java:99)上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'在com.example.javalearn.Fragment.ChatFragment$2.onBindViewHolder(ChatFragment.java:96)
错误发生在以下行:
if(model.getUserID().equals(mUser.getUid()))
FirebaseRecyclerAdapter<Chat, ChatMyViewHolder> adapter = new FirebaseRecyclerAdapter<Chat, ChatMyViewHolder>(options)
下面是用于加载消息的代码:
ChatFragment.java
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chat, container, false);
messageRecycleView = view.findViewById(R.id.chatMessageListRv);
messageRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
editTextInputMessages = view.findViewById(R.id.chatInputMessageEt);
imageButtonSendMessage = view.findViewById(R.id.chatSendMessageImgBtn);
circleImageViewUserProfilePic = view.findViewById(R.id.chatUserProfileImageAppBar);
textViewUserName = view.findViewById(R.id.chatUserProfileNameAppBar);
Bundle args = getArguments();
OtherUserId = args.get("userid").toString();
mUserRef = FirebaseDatabase.getInstance().getReference().child("users");
messageRef = FirebaseDatabase.getInstance().getReference().child("Messages");
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
LoadOtherUser();
LoadMessage();
imageButtonSendMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendMessage();
}
});
return view;
}
加载消息的方法:
private void LoadMessage() {
FirebaseRecyclerOptions<Chat> options = new FirebaseRecyclerOptions.Builder<Chat>().setQuery(messageRef.child(mUser.getUid()).child(OtherUserId), Chat.class).build();
FirebaseRecyclerAdapter<Chat, ChatMyViewHolder> adapter = new FirebaseRecyclerAdapter<Chat, ChatMyViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ChatMyViewHolder holder, int position, @NonNull Chat model) {
if(model.getUserID().equals(mUser.getUid())){
holder.friendProfilePic.setVisibility(View.GONE);
holder.friendMessage.setVisibility(View.GONE);
holder.myProfilePic.setVisibility(View.VISIBLE);
holder.myMessage.setVisibility(View.VISIBLE);
holder.myMessage.setText(model.getMessage());
} else {
holder.friendProfilePic.setVisibility(View.VISIBLE);
holder.friendMessage.setVisibility(View.VISIBLE);
holder.myProfilePic.setVisibility(View.GONE);
holder.myMessage.setVisibility(View.GONE);
holder.friendMessage.setText(model.getMessage());
Picasso.get().load(OtherUserImageUrl).placeholder(R.drawable.ic_baseline_person_24).into(holder.friendProfilePic);
}
}
@NonNull
@Override
public ChatMyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_messagetext, parent, false);
return new ChatMyViewHolder(view);
}
};
adapter.startListening();
messageRecycleView.setAdapter(adapter);
}
聊天类:
public class Chat {
public String message, status, userID;
public Chat(){
}
public Chat(String message, String status, String userID) {
this.message = message;
this.status = status;
this.userID = userID;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
}
聊天的ViewHolder:
public class ChatMyViewHolder extends RecyclerView.ViewHolder {
public CircleImageView friendProfilePic, myProfilePic;
public TextView friendMessage, myMessage;
public ChatMyViewHolder(@NonNull View itemView) {
super(itemView);
friendProfilePic = itemView.findViewById(R.id.friendMessageProfileImageCiv);
myProfilePic = itemView.findViewById(R.id.yourMessageProfileImageCiv);
friendMessage = itemView.findViewById(R.id.friendMessageTv);
myMessage = itemView.findViewById(R.id.yourMessageTv);
}
}
1条答案
按热度按时间kkbh8khc1#
这是你想要的输出吗?
如果是,这里是布局listitem_messagetext. xml的更改代码。