Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button setonclicklistener error

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?

like image 978
chandu Avatar asked Aug 23 '10 05:08

chandu


People also ask

Why my setOnClickListener is not working?

You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Save this answer.

What is setOnClickListener this?

One of the most usable methods in android is setOnClickListener method which helps us to link a listener with certain attributes. setOnClickListener is a method in Android basically used with buttons, image buttons etc. You can initiate this method easily like, public void setOnClickListener(View.OnClickListner)

What is setOnClickListener new view OnClickListener ()?

setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.

What is setOnClickListener Kotlin?

Kotlin setOnClickListener for Button Android Button widget is a UI element generally used to receive user actions as input. You can click on a Button, long press, etc. In this tutorial, we shall learn to set OnClickListener for Button.


1 Answers

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.

like image 82
krunal shah Avatar answered Sep 21 '22 13:09

krunal shah