这是我的项目聊天应用程序。问题是,当我在android上运行该应用程序时,尝试从Firebase数据库检索聊天。
错误:
E / Android运行时:致命异常:主要 流程:com.example.chatapp,PID:24371 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)' 在com.example.chatapp.Message_chat $ 4.onDataChange(Message_chat.java:150)
Message_chat.java:150
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
chat.getReceiver().equals(userid) && chat.getSender().equals(myid)) {
mList.add(chat);
}
源代码错误
如果(chat.getReceiver()。equals(myid)&& chat.getSender()。equals(userid)|| chat.getReceiver()。equals(userid)&& chat.getSender()。equals(myid)){ mList.add(聊天); }
错误
private void readMessage (final String myid, final String userid, final String imageurl){
mList = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("Chats");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Chat chat = snapshot.getValue(Chat.class);
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
chat.getReceiver().equals(userid) && chat.getSender().equals(myid)) {
mList.add(chat);
}
messageAdapter = new MessageAdapter(Message_chat.this, mList, imageurl);
recyclerView.setAdapter(messageAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
列表项
public class Chat {
private String sender, receiver, message;
public Chat(String sender, String receiver, String message) {
this.sender = sender;
this.receiver = receiver;
this.message = message;
}
public Chat() {
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
适配器
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
private static final int MSG_TYPE_LEFT = 0;
public static final int MSG_TYPE_RIGHT = 1;
private Context context;
private List<Chat> chatList;
private String imagerurl;
private FirebaseUser firebaseUser;
public MessageAdapter(Context context, List<Chat> chatList, String imagerurl){
this.context = context;
this.chatList = chatList;
this.imagerurl = imagerurl;
}
@NonNull
@Override
public MessageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(context).inflate(R.layout.chatmsg_right, viewGroup, false);
return new MessageAdapter.ViewHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.chatmsg_left, viewGroup, false);
return new MessageAdapter.ViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull MessageAdapter.ViewHolder viewHolder, int i) {
Chat chat = chatList.get(i);
viewHolder.show_message.setText(chat.getMessage());
if (imagerurl.equals("default")){
viewHolder.profile_image.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(context).load(imagerurl).into(viewHolder.profile_image);
}
}
@Override
public int getItemCount() {
return chatList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView show_message;
public ImageView profile_image;
public ViewHolder(View itemView){
super(itemView);
show_message = itemView.findViewById(R.id.show_messageR);
profile_image = itemView.findViewById(R.id.profile_imageR);
}
}
@Override
public int getItemViewType(int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (chatList.get(position).getSender().equals(firebaseUser.getUid())){
return MSG_TYPE_RIGHT;
}else {
return MSG_TYPE_LEFT;
}
}
}
这是消息聊天(聊天活动)中的全部代码
通过导入使用de.hdodenhof.circleimageview.CircleImageView的“圆形图像视图”;
public class Message_chat extends AppCompatActivity {
CircleImageView profileimage;
TextView username;
FirebaseUser firebaseUser;
DatabaseReference databaseReference;
ImageButton btnsendMessage;
EditText messageBox;
MessageAdapter messageAdapter;
List<Chat> mList;
RecyclerView recyclerView;
Intent intentMsgChat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_chat);
Toolbar toolbar = findViewById(R.id.toolbarMsgChat);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
recyclerView = findViewById(R.id.recyclerviewMsgChat);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
profileimage = findViewById(R.id.imageprofileMsgChat);
username = findViewById(R.id.usernameMsgChat);
btnsendMessage = findViewById(R.id.btnsendMsgChat);
messageBox = findViewById(R.id.boxmessageMsgChat);
intentMsgChat = getIntent();
final String userid = intentMsgChat.getStringExtra("UserID");
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
btnsendMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = messageBox.getText().toString();
if (!msg.equals("")) {
setSendMessage(firebaseUser.getUid(), userid, msg);
} else {
Toast.makeText(Message_chat.this, "Message is empty", Toast.LENGTH_SHORT).show();
}
messageBox.setText("");
}
});
databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Userprofile userprofile = dataSnapshot.getValue(Userprofile.class);
assert userprofile != null;
username.setText(userprofile.getUsername());
if (userprofile.getImageURL().equals("default")) {
profileimage.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(Message_chat.this).load(userprofile.getImageURL()).into(profileimage);
}
readMessage(firebaseUser.getUid(), userid, userprofile.getImageURL());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void setSendMessage (String sender, String receiver, String message){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("Sender", sender);
hashMap.put("Receiver", receiver);
hashMap.put("Message", message);
reference.child("Chats").push().setValue(hashMap);
}
private void readMessage (final String myid, final String userid, final String imageurl){
mList = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("Chats");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
Chat chat = snapshot.getValue(Chat.class);
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid) ||
chat.getReceiver().equals(userid) && chat.getSender().equals(myid)) {
mList.add(chat);
}
messageAdapter = new MessageAdapter(Message_chat.this, mList, imageurl);
recyclerView.setAdapter(messageAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}