android - FindViewById returning null? -


i trying populate listview array. however, when try , find listview in java code, findviewbyid function returns null.

here xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@drawable/androidbg_dark"     tools:context=".diaryactivity" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical" >          <listview             android:id="@+id/lstappts"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_weight="5"             android:textcolor="#ffffff" ></listview>          <linearlayout             android:layout_width="match_parent"             android:orientation="horizontal"             android:layout_weight="1"              android:layout_height="64dp">              <button                 android:id="@+id/btnother"                 style="?android:attr/buttonbarbuttonstyle"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:layout_margin="2dip"                 android:layout_weight="1"                 android:background="@drawable/menu_button"                 android:onclick="newappointment"                 android:textcolor="#ffffff" />              <button                 android:id="@+id/btnother2"                 style="?android:attr/buttonbarbuttonstyle"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:layout_margin="2dip"                 android:layout_weight="1"                 android:background="@drawable/menu_button"                 android:onclick="newappointment"                 android:textcolor="#ffffff" />              <button                 android:id="@+id/btnnewappt"                 style="?android:attr/buttonbarbuttonstyle"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:layout_margin="2dip"                 android:layout_weight="1"                 android:background="@drawable/menu_button"                 android:onclick="newappointment"                 android:text="@string/newappt"                 android:textcolor="#ffffff" />         </linearlayout>      </linearlayout>  </relativelayout> 

and here offending code:

listview lstappts = (listview) findviewbyid(r.id.lstappts); string[] data = { "appointment 1", "appointment 2", "appointment 3" };  arrayadapter adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, data); lstappts.setadapter(adapter); 

to knowledge, referencing listview correctly. named 'lstappts' in xml, , in findviewbyid function, find using r.id.lstappts. when debug code breakpoints, can see the lstappts object null. why this?

it seems you're trying call findviewbyid() on constructor. in android should use oncreate() method init activity instead of constructor.


Comments