Der Aufruf von findViewById in Fragment zum Suchen einer Schaltfläche gibt null zurück

Ich bin neu in der Android-Entwicklung und ich versuche, eine kleine, einfache Anwendung zu schreiben. Diese Anwendung sollte nichts weiter tun, als den Text aus 2 EditTexts zu lesen, und wenn der Benutzer die Schaltfläche "Senden" drückt, sollte er den Text in 2 TextViews schreiben. Jede dieser TextViews befindet sich in einem Fragment.

Ich kann das onClickEvent aber nicht zum Button hinzufügen, da findViewById (R.id.sendTextButton) immer null zurückgibt. Ich habe versucht, diese App zum Laufen zu bringen, konnte aber noch keine Lösung finden.

Hier sind die relevanten CodeBlocks:

Die onCreateView-Funktion des Fragments, das die erste Eingabe verarbeiten soll (HeadlineFragment.java):

Button sendButton = null;
View inflatedView = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);

    sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);

    if(sendButton == null)
    {
        Log.d("debugCheck", "HeadFrag: sendButton is null");
        return inflatedView;
    }
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString();
            ((TextView) v.findViewById(R.layout.headline_view)).setText(headline);
        }
    });
    return inflatedView;
}

Die XML-Datei mit dem Layout (activity_main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/enterHeadlineText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enterHeadlineTextHint"
            android:maxLines="1"
            android:inputType="textCapSentences"
        />

        <EditText
            android:id="@+id/enterMessageText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="@string/enterMessageTextHint"
            android:maxLines="1"
            android:inputType="textCapSentences"
            />

        <Button
            android:id="@+id/sendTextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/sendTextButton"
            />



        <fragment android:name="com.example.mysecondapplication.HeadlineFragment"
            android:id="@+id/headlineFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            />

        <fragment android:name="com.example.mysecondapplication.MessageFragment"
            android:id="@+id/messageFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            />



</LinearLayout>

Und die XML-Datei mit der TextView, die den Text enthalten soll (headline_view.xml):

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/headline_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:text="@string/hello_world"
/>

Antworten auf die Frage(4)

Ihre Antwort auf die Frage