Reputation: 387
I am trying to get a TextView by ID in my listener class which handles a spinner's onItemSelected event. In my main Activity I can call findViewById(R.id.textView1) and it works fine. But if I call the same function in my spinner listener class it always returns null and forces the app to close.
Anyone know why findViewById works in my main Activity but not in my listener class?
Here is the code from my main activity. It is called when a button is clicked:
public void something ()
{
setContentView(R.layout.spinnerLayout);
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
And here is the code for MyOnItemSelectedListener:
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
TextView tv = view.findViewById(R.id.myTextView);
// I want to set the text of this text view to the value selected using the spinner
tv.setText("Something");
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
What do I need to do to get "TextView tv = view.findViewById(R.id.myTextView);" to work?
Thanks!!!
Upvotes: 2
Views: 9219
Reputation: 81
This is such an old unanswered question but it is the closest match to my problem which I just fixed by using getRootView(). The problem was that view
inside the OnItemSelectedListener
refers to the View object of the text shown inside the spinner.
Since findViewById
looks for descendants of the current view, it won't find the textview you are looking for.
Replacing
TextView tv = view.findViewById(R.id.myTextView);
// I want to set the text of this text view to the value selected using the spinner
tv.setText("Something");
with
TextView tv = view.getRootView().findViewById(R.id.myTextView);
// I want to set the text of this text view to the value selected using the spinner
tv.setText("Something");
Would make findViewById
search for your textview from the root which is probably what you were expecting to happen.
Upvotes: 0
Reputation: 1486
In my main Activity I can call findViewById(R.id.textView1) and it works fine. But if I call the same function in my spinner listener class it always returns null and forces the app to close.
As I am new to Android too, I might be wrong, but I have a few things that I am thinking about:
More code snippets would be good. nyyrikki
Upvotes: 0
Reputation: 6160
If you have a setContentView
as mentioned in the other posts you might need to specify the view in which to find the textview:
TextView tv = (TextView)view.findViewById (R.id.textView1);
Upvotes: 1
Reputation: 10584
Where are you setting the content view? When you are extending Activity
, there should be a method onCreate
in which you need to setContentView(R.id.textview1)
.
Upvotes: 1
Reputation: 200090
In order for findViewById
to work you must have previously called setContentView
Upvotes: 7