I´m new to android development and I´m trying to write a small, simple Application. This application should do nothing more than reading the text from 2 EditTexts and when the user presses the "send" button it should write the text to 2 TextViews. Each of these TextViews is in one Fragment.
But I can´t add the onClickEvent to the Button, because findViewById(R.id.sendTextButton) always returns null. I have tried to get this App working but I wasn´t able to find any solution by now.
Here are the relevant codeBlocks:
The onCreateView function of the fragment, that should handle the first input(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;
}
The xml file with the 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>
And the xml file with the TextView, that should contain the text (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"
/>
this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);
you are looking for a Button with id sendTextButton
inside headline_view.xml
. But from the layout you posted there is no Button with id sendTextButton. That`s way you get null
That's the problem
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);
}
});
the View v is the view clicked, so of coure v.findViewById() doesn't work..
to achieve your target you can declare youre EditText and TextView global, in onCreate() use the inflater to instantiate them and onClick method you can use them!
Your button is inside activity_main.xml
, but you're inflating R.layout.headline_view
:
this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
You have to set the onClickListener
inside the Activity from which you are managing the fragments and not inside the fragment, if the Button is not in the layout that your fragment is inflating.
来源:https://stackoverflow.com/questions/17996339/call-of-findviewbyid-in-fragment-to-find-a-button-returns-null