Null.pointer.Exception when I invoke setOnClickListener

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Null.pointer.Exception when I invoke setOnClickListener

Postby aneuryzma » Thu Jan 14, 2010 8:17 pm

Hi,

I get Null.pointer.Exception when I invoke setOnClickListener.

...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onCreate(Bundle savedInstanceState) {
  2.  
  3.  
  4.  
  5.                 super.onCreate(savedInstanceState);
  6.  
  7.                
  8.  
  9.  
  10.  
  11.         Button addTagB = (Button) findViewById(R.id.Button03);
  12.  
  13.         Button retrieveTagB = (Button) findViewById(R.id.Button04);
  14.  
  15.  
  16.  
  17.         try{
  18.  
  19.                
  20.  
  21.         addTagB.setOnClickListener(new View.OnClickListener() {
  22.  
  23.        
  24.  
  25.                 public void onClick(View view) {
  26.  
  27.  
  28.  
  29.                         TagMode = 0;
  30.  
  31.                         Log.v(TAG, "addTag button pressed");
  32.  
  33.                                 //mCamera.autoFocus(cb);
  34.  
  35.                 }
  36.  
  37.         });
  38.  
  39.        
  40.  
  41.         retrieveTagB.setOnClickListener(new View.OnClickListener() {
  42.  
  43.                 public void onClick(View view) {
  44.  
  45.  
  46.  
  47.                         TagMode = 1;
  48.  
  49.                         Log.v(TAG, "retrieveTag button pressed");
  50.  
  51.                         //mCamera.autoFocus(cb);
  52.  
  53.                 }
  54.  
  55.         });
  56.  
  57.        
  58.  
  59.                 } catch (Exception e) {
  60.  
  61.                         e.printStackTrace();
  62.  
  63.                 }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


This is the xml:

Code: Select all
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:orientation="vertical">
   
   <SurfaceView android:id="@+id/surface_camera_tag"
      android:layout_width="fill_parent" android:layout_height="fill_parent"
      android:layout_weight="0.1">
   </SurfaceView>
   
   <LinearLayout
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="0.9">
   
   <Button android:text="Add Tag" android:id="@+id/Button03"
      android:layout_width="150px" android:textSize="20px"
      android:layout_height="fill_parent">
   </Button>
   
   <Button android:text="Retrieve Tag" android:id="@+id/Button04"
      android:layout_width="150px" android:textSize="20px"
      android:layout_height="fill_parent">
   </Button>
   
   </LinearLayout>
   
</LinearLayout>


thanks
aneuryzma
Developer
Developer
 
Posts: 28
Joined: Wed Jan 06, 2010 10:46 am

Top

Postby jwei512 » Fri Jan 15, 2010 7:31 am

What line does it say it's hitting the null pointer exception? Have you checked that your two buttons are not null when you try to inflate them by id? Looking at your XML, they shouldn't be, but maybe something happened...
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby aneuryzma » Fri Jan 15, 2010 9:01 am

yes they are null, but they shouldn't be...
I'm not sure my app is reading the xml file.. for each activity I specified a new xml file, in which I specify the layout and I reference the elements IDs.

I have 3 activities, for the other 2 activities all objects are referenced correctly with R.id.objectName, but for that buttons something go wrong.

The activity containing the buttons, is a children of another activity in my code, since they share the same functionalities. Should I maybe move everything in one xml file ? How does it work ?

thanks
aneuryzma
Developer
Developer
 
Posts: 28
Joined: Wed Jan 06, 2010 10:46 am

Postby icobasco » Fri Jan 15, 2010 9:20 am

It seems you forgot to put the setContentView....

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        
  2.  
  3.  
  4.  
  5. public void onCreate(Bundle savedInstanceState) {
  6.  
  7.  
  8.  
  9.           super.onCreate(savedInstanceState);        
  10.  
  11.  
  12.  
  13.         setContentView(R.layout.my_layout_xml);  // You missed this
  14.  
  15.  
  16.  
  17.         Button addTagB = (Button) findViewById(R.id.Button03);
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


before finding out the view ;)
The null is correct cause you asked Android to find out something (button) but you didn't specify where...

Just replace my_layout_xml with your layout xml file name.
User avatar
icobasco
Senior Developer
Senior Developer
 
Posts: 108
Joined: Thu Jun 04, 2009 11:12 am
Location: Italy

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests