Free Android CakePHP GMaps Articles by Bali Web Design

July 14, 2009

Why does findViewById return null?

Filed under: android,java — admin @ 12:10 am

I have been confusing about this problem when doing a project using google map. I believe some of you have this problem also. The problem is simple about a findViewById function on Activity or View always returning Null value. I have the following code on my layout

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>

<com.google.android.maps.MapView
android:id=”@+id/mapView”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:enabled=”true”
android:clickable=”true”
android:apiKey=”0QGdlm3NNz0KF5zSUTn5FvObCU2EUn7Dim25Sjw”
/>

<Button id=”@+id/btnViewPOI”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_alignParentRight=”true”
android:visibility=”visible”
android:text=”List” />

</RelativeLayout>

on my Activity class, i was try to find a reference to button btnViewPOI. Here is a part of the code in my Activity class

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button btnViewPOI = (Button) findViewById(R.id.btnViewPOI);
if(btnViewPOI == null) Log.d(“null”, “is null”);

And i always get null value for btnViewPOI. I spend a day to figure out the problem and almost give up, but thanks god that i notice if the layout parser did not understand the attribute id, we must use a complete android:id. And i change it and its work, no more returning a null value.

The code for my button in layout xml file should look like this

<Button
android:id=”@+id/btnViewPOI”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_alignParentRight=”true”
android:visibility=”visible”
android:text=”List” />

So i try to summary the important thing to remember if findViewById return a null value:

  1. if you use xml based layout, you must add this line before run findViewById.
    setContentView(R.layout.main);
  2. the id that you can use is only the id that define on your layout for each activity. To check the id you can find it on R class
  3. remember to use complete android:id to define an id to a view

That all, i hope that you can solve the problem when findViewById return a null value.


10 Comments »

  1. thanks dude! I’ve been trying to get a reference to a TextView that I have as part of a custom xml list layout for hours (I was missing the android bit from id on the TextView element)
    Eric

    Comment by Eric — October 23, 2009 @ 6:17 pm

  2. Oye! Thanks so much! I’ve been struggling with this for hours. damn setContentView…

    Comment by Michael — June 13, 2010 @ 3:21 pm

  3. Thanks so much. I have been stuck on this for quite a while.

    Comment by RockU — October 12, 2010 @ 2:26 am

  4. Excellent: problem solved. I was using name=’@+id/var’ instead of android:id=’@id/var’ (it still generated ids in the R.java file which threw me off).

    Comment by GYI — January 12, 2011 @ 12:02 am

  5. Hi,
    I am new to Android and I’m trying to figure it out, why I have to call setContentView before findViewById? All inner classes from R.class are static, so they should be initalized before runtime. I can’t find answer and your blog was first site where is article about that.

    Comment by Slawek — May 24, 2011 @ 12:31 pm

  6. Thanks so much! I was using “android:id” instead of “android:name” to solving this problem. thank your blog~~~

    Comment by xian.wang — September 21, 2011 @ 11:53 am

  7. Thanks a lot man!!! mine i was using android:id=”@+id/okbutton” from the starting but problem was that i was using a relative layout and had the other component id references with the “+” sign and hence this stupid parser was always returning null when i do findViewById

    Comment by Maverick — September 29, 2011 @ 11:48 pm

  8. When you’re code is ok, and the null reference is returned, you need to clean your project.
    Note: To clean the project means to clear out the cache (like the web browser when a page is loaded), so the new changes will be there.
    In the menu (eclipse menu) Project->Clean…
    When ask about about the project to Clean, you select “Clean all projects” or “Clean projects selected below”.
    Press the ok button and is done, the project will run ok.

    Comment by dexterminio — October 15, 2011 @ 12:50 pm

  9. Thank you for the post, it got me on the right track.

    I used the following in main.xml:
    android:id=”@+id/deviceidtext”

    and got the same null pointer.

    After changing the code to:
    android:id=”@+id/devicetext”

    The program was able to retrieve the correct view.

    It seems that the parser looks into the naming string aswell for “id” and cannot resolve the situation

    Comment by lonewolf — November 15, 2011 @ 3:59 pm

  10. I too saw this issue in 1.6 … thanks for pointing out the solution!

    Comment by jonaskinny — July 19, 2012 @ 11:51 am

RSS feed for comments on this post. TrackBack URL

Leave a comment