findViewById
is not a global search for a View
. It only looks in specific places. If no View
is specified (as in your myView.findViewById() example) findViewById
will look in the content view for the Activity
or Fragment
you are calling it from. Otherwise it will look within the specified view.
You will get null if you are looking for the view in the wrong place - i.e. what you are looking for is not in the place you're searching.
Here's an example: if you're using a ListView
whose items you are controling with an Adapter
you would need to call findViewbById
on the inflated layout for the indivdual items in order to set their child views. Why? Because a ListView
starts out empty. If you called findViewById
within the general ListActivity
you would be looking for list item fields in the wrong place, i.e. the content view for the activity - not the layout that will eventually be inflated for each list item.