Я делаю приложение чат. Прописал адаптер, передачу и приём данных но приложение вылетает
Код активити чата
public class ChatActivity extends AppCompatActivity {
private EditText edit_chat;
private ImageView image_go_message,imageView_dialog;
private Dialog chatdialog;
private DatabaseReference databaseReference;
private EditText textget;
private FirebaseAuth mAuth;
private RecyclerView recycle;
private ArrayList<String> messages=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
edit_chat=(EditText) findViewById(R.id.edit_chat);
imageView_dialog=(ImageView) findViewById(R.id.imageView_dialog);
image_go_message=(ImageView) findViewById(R.id.image_go_message);
mAuth = FirebaseAuth.getInstance();
textget=(EditText) findViewById(R.id.textget);
databaseReference = FirebaseDatabase.getInstance().getReference().child("User").child("d");
Adapter adapter=new Adapter(this,messages);
recycle=(RecyclerView) findViewById(R.id.recycle);
recyclemanwoman.setLayoutManager(new LinearLayoutManager(this));
///adapter///
recycle.setAdapter(adapter);
chatdialog=new Dialog(ChatActivity.this);
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
String msg=snapshot.getValue(String.class);
messages.add(msg);
adapter.notifyDataSetChanged();
recycle.smoothScrollToPosition(messages.size());
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
imageView_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chatdialogShow();
}
});
image_go_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ValidateAndSaveOnlyInfo();
}
});
}
private void chatdialogShow() {
chatdialog.setContentView(R.layout.dialog_exit);
chatdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button button_chat_end=chatdialog.findViewById(R.id.button_chat_end);
Button button_back_chat=chatdialog.findViewById(R.id.button_back_chat);
button_back_chat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chatdialog.dismiss();
}
});
button_chat_end.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
chatdialog.show();
}
private void ValidateAndSaveOnlyInfo() {
if (TextUtils.isEmpty(edit_chat.getText().toString())) {
Toast.makeText(this, "Введите текст", Toast.LENGTH_SHORT).show();
} else {
HashMap<String, Object> userMap = new HashMap<>();
userMap.put("uid", mAuth.getCurrentUser().getUid());
userMap.put("text", edit_chat.getText().toString());
databaseReference.child(mAuth.getCurrentUser().getUid()).updateChildren(userMap);
edit_chat.setText("");
}
}
}
Код адаптера
public class Adapter extends RecyclerView.Adapter<ChatHolder> {
ArrayList<String> messages;
LayoutInflater inflater;
public Adapter(Context context,ArrayList<String> messages) {
this.messages = messages;
this.inflater=LayoutInflater.from(context);
}
@NonNull
@Override
public ChatHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.item_container_priem_message,parent,false);
return new ChatHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ChatHolder holder, int position) {
String msg=messages.get(position);
holder.message.setText(msg);
}
@Override
public int getItemCount() {
return messages.size();
}
}
Код Holder
public class ChatHolder extends RecyclerView.ViewHolder {
TextView message;
public ChatHolder(View itemView){
super(itemView);
message=itemView.findViewById(R.id.priem_message);
}
}
Вот задумка была такая что я пропишу два отдельных адаптера под сообщения которые приходят и который пишет сам пользователь. Но при первом тесте вылетела.
Синим отмечена 71строчка активити.Вот ошибка:
FATAL EXCEPTION: main
Process: anonimchatguest.app, PID: 7011
com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at anonimchatguest.app.ChatActivity$1.onChildAdded(ChatActivity.java:71)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:79)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8056)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)