Free Android CakePHP GMaps Articles by Bali Web Design

Google

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.

2 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

RSS feed for comments on this post. TrackBack URL

Leave a comment