D
D
DARKENN2020-02-28 16:15:05
Java
DARKENN, 2020-02-28 16:15:05

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("");

    }

}


Activity code
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)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2020-02-28
@DARKENN

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.

P
priority, 2020-02-28
@priority

The reason for the error is that you call the getText() method on a thing that is null, that is, at the time of execution of that piece of code sendText == null
You can also use a debugger and check yourself, how it happened

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question