Wywołanie findViewById w Fragmentie w celu znalezienia przycisku zwraca wartość null
Jestem nowy w rozwoju Androida i próbuję napisać małą, prostą aplikację. Ta aplikacja nie powinna robić nic więcej, jak tylko czytać tekst z 2 EditTextów, a gdy użytkownik naciśnie przycisk „wyślij”, powinien napisać tekst do 2 TextViewów. Każdy z tych TextViews jest w jednym fragmencie.
Ale nie mogę dodać zdarzenia onClickEvent do przycisku, ponieważ findViewById (R.id.sendTextButton) zawsze zwraca wartość null. Próbowałem uruchomić tę aplikację, ale nie byłem w stanie znaleźć żadnego rozwiązania.
Oto odpowiednie kodyBlocks:
Funkcja onCreateView fragmentu, który powinien obsługiwać pierwsze wejście (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;
}
Plik xml z układem (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>
I plik xml z TextView, który powinien zawierać tekst (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"
/>