6

I am having a problem right now with setOnClickListener.

When i put this following line:

button.setOnClickListener(this);

And run the application then it does not run and shows a message that "Application closed forcefully".

Could you please help me how I can set button onclick event in Android 2.2?

bool.dev
  • 17,508
  • 5
  • 69
  • 93
chandu
  • 61
  • 1
  • 1
  • 2

6 Answers6

7

See if the code below works for you...

button.setOnClickListener(new OnClickListener() {              
  @Override
  public void onClick(View v) 
  {
      Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show();
  }    
});      

Remember to add }); at the end.

masarapmabuhay
  • 466
  • 1
  • 9
  • 15
krunal shah
  • 16,089
  • 25
  • 97
  • 143
  • 3
    thank you very much for the answers. I got the solution. Actually i was putting the code of the button onclicklistener in Splash screen coding page. Thanks – chandu Aug 25 '10 at 05:49
4

another possible reason ( happened to me ) is your activity must implement OnClickListener

public class MainActivity extends Activity implements OnClickListener ...
biegleux
  • 13,179
  • 11
  • 45
  • 52
apo
  • 41
  • 1
4

For defining button click event in android, You can try the below code:

public class Main_Activity extends Activity {


    private Button myButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myButton = (Button) findViewById(R.id.Button01);
    myButton.setOnClickListener(new Button_Clicker());
}

class Button_Clicker implements Button.OnClickListener
{
    @Override
    public void onClick(View v) {

       if(v==myButton)
       {
                Toast.makeText(v.getContext(), "Hello!! button Clicked", Toast.LENGTH_SHORT).show();

       }    
}
}

}

Bogdan M.
  • 2,161
  • 6
  • 31
  • 53
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
2

Type View.onClickListener instead of Button on ClickListener

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
sami
  • 21
  • 1
2

Although it's been a long time, thought it might help others who have this problem, it took me many trials to get it right. But i think what finally solved my problem was setting the clickable attribute of a button in the layout's xml to true.
Code sample:

<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:clickable="true">
</Button>

Further, if you would've looked at the DDMS perspective, you would've seen that the cause of the error was NullPointerException, which ofcourse was showing because clickable wasn't set. Correct me if i am wrong.

bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • 1
    I would like to correct myself. I dont think the above is the correct solution. If anyone is getting a NullPointerException, even though button has been correctly assigned using findViewById, i.e `Button button = (Button)findViewById(R.id.somebutton);` , then i would suggest, to clean current build and rebuild. It happened to me a few times. – bool.dev May 30 '11 at 15:47
0

Check if in the class definition there is implements OnClickListener

Re MiDa
  • 199
  • 1
  • 8