Код фрагмента
package com.p0001.parallax.Chat;
import android.app.Service;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.p0001.parallax.Adapters.ChatAdapter;
import com.p0001.parallax.Models.ChatModel;
import com.p0001.parallax.R;
import java.util.ArrayList;
import java.util.List;
public class ChatActive extends Fragment {
private FirebaseDatabase database;
private ImageButton button_open_chat;
private RecyclerView mMessagesRecycler;
private List<ChatModel> result = new ArrayList();
private List<String> keyList = new ArrayList<String>();
private DatabaseReference myBase;
private EditText sendText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_chat_active, container, false);
database = FirebaseDatabase.getInstance();
myBase = database.getReference("chat");
sendText = inflate.findViewById(R.id.message_input);
mMessagesRecycler = inflate.findViewById(R.id.chat);
mMessagesRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
final ChatAdapter dataAdapter = new ChatAdapter(result);
mMessagesRecycler.setAdapter(dataAdapter);
myBase.addChildEventListener(new ChildEventListener() {
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
result.add(dataSnapshot.getValue(ChatModel.class));
int insertIndex = result.size();
dataAdapter.notifyItemInserted(insertIndex);
keyList.add(dataSnapshot.getKey());
mMessagesRecycler.smoothScrollToPosition(result.size());
}
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
int index = keyList.indexOf(dataSnapshot.getKey());
result.remove(index);
keyList.remove(index);
dataAdapter.notifyDataSetChanged();
}
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Service.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);
sendText.requestFocus();
if(2 == 1) {
send_message();
}
return inflate;
}
public void send_message() {
String text = sendText.getText().toString();
FirebaseDatabase.getInstance().getReference().child("chat").push().setValue(new ChatModel(
text, "Админ"));
sendText.setText("");
}
}
Код активити
buttonSend.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
ChatActive chat = new ChatActive();
chat.send_message();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ChatPassive()).commit();
buttonSend.setVisibility(View.INVISIBLE);
button_open_chat.setVisibility(View.VISIBLE);
}
});
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.p0001.parallax, PID: 5298
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.p0001.parallax.Chat.ChatActive.send_message(ChatActive.java:98)
at com.p0001.parallax.Game$1.onClick(Game.java:102)
at android.view.View.performClick(View.java:7346)
at android.view.View.performClickInternal(View.java:7312)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27794)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7099)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)