Android findViewById returns null when used in class other than main Activity
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!!!
In order for findViewById
to work you must have previously called setContentView
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)
.
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);
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:
- why your listener extends an activity?
- if you call findByView again, android has to go down the tree again to find the correct ID, just save the textView once you found it in a field variable
- if that is really an activity, did you fire an correct Intent in your main activity to start it?
- did you add that activity to the manifest.xml?
- ...as the other people said already, it might be the wrong context.
More code snippets would be good. nyyrikki
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.
精彩评论