Why Does findViewById() Return null When I Call It?

First, make sure you are calling it on the right object. Both Activity and ViewGroup support findViewById(), and so you want to make sure you are calling it on the object that should contain the object with the specified ID.

Next, if you are calling it on an Activity, make sure you call it after you call setContentView(). Until setContentView() is called, all calls to findViewById() will return null, since the layout has not yet been associated with the Activity.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License