Cameron Thompson
Cameron Thompson

Reputation: 9

Android Null Pointer Exception Button

Ok this may seem like a pointless example but if I can figure this out then the program I am trying to make will work. So I have two activities test and test two each with one button.

Test 1:

package thompson.cameron.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Test extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View button = findViewById(R.id.testButton);
        button.setOnClickListener(this);
    }

    public void onClick(View v){
     Intent i = new Intent(this, Test2.class);
     startActivity(i);
     }

}

and test2

package thompson.cameron.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Test2 extends Activity implements OnClickListener {

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

  View test = findViewById(R.id.testButton);
  test.setOnClickListener(this);
 }

 public void onClick(View v){
  switch (v.getId()){
  case R.id.testButton:
   System.exit(1);
  }
 }

}

When I click the button on Test it is supposed to launch test2 however it is at this point I get an null pointer exception that I have narrowed down to test.setOnClickListener(this); line of code. Below are my two xml files for the layout. I can get the button to work when I only have one activity but as soon as I add a second activity with a different layout file it all falls apart

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
 android:id="@+id/testButton"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TEST TEST TEST"/>

</LinearLayout>

main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
 android:id="@+id/testButton"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TEST2 TEST2 TEST2"/>

</LinearLayout>

I'm still new at Android programming so thanks for all your help.

Upvotes: 0

Views: 15722

Answers (10)

Anju
Anju

Reputation: 9479

In your test.java file give:

implements View.OnClickListener

Initialize your button as:

Button testButton = (Button) findViewById(R.id.testButton);

and inside your onClick method, check whether you are clicking button:

if(v == testButton) {
//give ur intent code
}

There are different ways to perform onClick functionality. One is the above method which I have mentioned. Another one is what ankit has mentioned. Third way is through your layout.

Inside your layout for your button tag, you may give as:

<Button android:id="@+id/testButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Click" android:onClick="onTestButtonClick" />

And inside your class just mention the below details for button:

public void onTestButtonClick(View view) {
     //give your intent code
}

You may refer to the link also: http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html

Upvotes: 2

Record413123
Record413123

Reputation: 130

  1. Open the Debug perspective in Eclipse
  2. Choose the 'Breakpoints' view tab.
  3. Select the 'Add Java Exception Breakpoint' and choose NullPointerException.
  4. Launch your activity, either by using 'Debug As...' or attaching the debugger to a running instance via DDMS.
  5. Execute the offending workflow. It will break on the line that caused the NullPointerException.

Upvotes: 2

Jitesh Dalsaniya
Jitesh Dalsaniya

Reputation: 1917

First of all in main.xml and main2.xml chage the button's ids like in below code.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
 android:id="@+id/testButton"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TEST TEST TEST"/>

</LinearLayout>

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
 android:id="@+id/testButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="TEST2 TEST2 TEST2"/>

</LinearLayout>

It throws nullpointerexception because of id confict with each other so in your java file use following code to find button.

In Activity 1

Button button = findViewById(R.id.testButton);
button.setOnClickListener(this);

and In Activity 2

Button button = findViewById(R.id.testButton1);
button.setOnClickListener(this);

Upvotes: 0

ankita gahoi
ankita gahoi

Reputation: 1562

You can try this.it may work.

package thompson.cameron.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Test extends Activity{

      private Button button;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = findViewById(R.id.testButton);
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                Intent i=new Intent().setClass(Test.this,Test2.class);
                startActivity(i);

            }
        });
    }



}

Upvotes: 0

Pushkar Raj
Pushkar Raj

Reputation: 99

Implement OnClickListener interface

and set button.setOnClickListener(this);

and override

public void onClick(View v) {


}

Upvotes: 1

Jose Carlos
Jose Carlos

Reputation: 11

The final solution is that you may modify the AndroidManifest.xml file, i finally solved my error in this link How to register a new activity in AndroidManifest.xml?

Upvotes: 0

Jose Carlos
Jose Carlos

Reputation: 11

I had the same problem, you may put the same content view that the button, setContentView(R.layout.main); if the button is in that content view, in other case, you will put:

setContentView(R.layout.buttoncontentview);
View button = findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v){ 
  Intent i = new Intent(this, Test2.class);
  startActivity(i); 
} 
setContentView(R.layout.main);

sorry for my bad english, but i'm spanish

Upvotes: 1

Andrew Goulart
Andrew Goulart

Reputation: 11

The issue here is that you haven't typecasted your views to buttons.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; // Needed to add this import for the button casting below


public class Test extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // I have changed View to Button and then typecasted
        // with the "(Button)" the return of findViewById

        Button button = (Button) findViewById(R.id.testButton);
        button.setOnClickListener(this);
    }

    public void onClick(View v){
        Intent i = new Intent(this, Test2.class);
        startActivity(i);
    }
}

Let me know if you have any issues with this. I just completed my first experiment through using the onClickListener implementation through the main class instead of individual anonymous listeners.

Andrew

Upvotes: 1

Abhinav
Abhinav

Reputation: 39952

I think your buttons IDs need to be different in different activities. R.id.testButton would refer to only one button.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104196

Make sure that both activities are register at the application's manifest file.

As a side note never call System.exit in your code. You can call finish() to close an Activity and this will bring at the front the previous Activity on the stack.

Upvotes: 1

Related Questions