Answer the question
In order to leave comments, you need to log in
What is the cause of the error (fragments)?
Fragment Code
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);
}
});
Answer the question
In order to leave comments, you need to log in
Well, it's obvious. You created a fragment, and at once you pull its method. Naturally, the view has not yet been created, because you have not committed it, and its life cycle has not even begun.
Don't do that, pulling the methods of a particular fragment from the outside is not a good idea.
Learn to read stack traces, everything is written there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question