findViewById allways return null

Put your problem here if it does not fit any of the other categories.

findViewById allways return null

Postby wonglik » Sun Feb 24, 2008 10:47 am

Hi All.

I have some strange problem. I have a layout in xml with id's assign to elements. While I try to get they using findViewById method I get null. Even in anddev tutorials made by copy and paste the sources from the site.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class M5test extends Activity {
  3.  
  4.  
  5.  
  6.     public void onCreate(Bundle icicle) {
  7.  
  8.         super.onCreate(icicle);
  9.  
  10.         setContentView(R.layout.test);
  11.  
  12.         Button okButton = (Button)findViewById(R.id.ok_button);
  13.  
  14.         Button clearButton = (Button)findViewById(R.id.clear_button);
  15.  
  16.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

and test.xml file :
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  
  3.  
  4.  <Button id="@+id/ok_button"
  5.  
  6.            android:layout_width="wrap_content"
  7.  
  8.            android:layout_height="wrap_content"
  9.  
  10.            android:layout_below="@id/keyword_entry"
  11.  
  12.            android:layout_alignParentRight="true"
  13.  
  14.            android:layout_marginLeft="10px"
  15.  
  16.            android:text="OK" />
  17.  
  18.  
  19.  
  20.     <Button id="@+id/clear_button"
  21.  
  22.          android:layout_width="wrap_content"
  23.  
  24.            android:layout_height="wrap_content"
  25.  
  26.            android:layout_toLeft="@id/ok_button"
  27.  
  28.            android:layout_alignTop="@id/ok_button"
  29.  
  30.            android:text="Clear" />
  31.  
  32.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


anybody experienced similar problem? Is is something related to m5?

regards
wonglik
Freshman
Freshman
 
Posts: 8
Joined: Tue Feb 12, 2008 6:35 pm
Location: Poland

Top

Solution

Postby wonglik » Sun Feb 24, 2008 12:29 pm

I have manage to solve this problem. In case somebody ever has similar problem , the problem was that
id attribute has been deprecated so solution is to replace id with android:id.
wonglik
Freshman
Freshman
 
Posts: 8
Joined: Tue Feb 12, 2008 6:35 pm
Location: Poland

Postby plusminus » Sun Feb 24, 2008 10:00 pm

Exactly. Since m5 it is [font=Courier New]android:id[/font].

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests