Skip to content Skip to sidebar Skip to footer

Android: Why Can't I Call Setonclicklistener Method On A Button Outside Of An Oncreate Method?

This is a newbie question but why can't I call setOnClickListener method on a button outside of an onCreate method? For example why can't I do this? Or maybe Eclipse just don't hav

Solution 1:

You can setonclicklistener in any function. But this is not your exact problem. Actual problem is

setContentView(R.layout.main_layout);

you can call findViewById function after setContentView. setContentView is used to set layout on an activity. Your layout contains different views like buttons etc. So if you will not set your layout then you cannot access your views by using findViewById and if you cannot access then you cannot use them.

So proper sequence will be:

1Set layout on your activity using setContentView.
2 Find id for your viewusing findViewById.
3Then use your views for your purposes.

Always concentrate on basics first.

One more problem is you are calling setOnClickListener outside any function. Outside any function static blocks, function declaration and assignments are only allowed. Below statement is neither of them. So it will never execute and callback will never occur.

button.setOnClickListener(newView.OnClickListener() {
     publicvoidonClick(View v) {
      // Do something in response to button click
  }

});

Solution 2:

onCreate is sort of your main(String[] args) function in normal Java. It is where you setup your code.Also see this link:-

Noob question about "Hello World" Android tutorial

This the right way:-

publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
    Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(newView.OnClickListener() {
 publicvoidonClick(View v) {
  // Do something in response to button click
 }
});
}

or

This is the another way to achieve your task:-

publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
    Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(onClick);
}


publicOnClickListener onClick= newOnClickListener() {

    @OverridepublicvoidonClick(View v) {

        // Do something in response to button click
    }
};

Solution 3:

Only after the setContentView(R.layout.main_layout); is called you get access to the components inside that view (i.e. the button). Because the declaration and instantation of the button is written in the main class context it happens before the setContentView.

Insert the button code section inside the onCreate and it will work.

Solution 4:

I think that is because you want the listeners to be added as soon as the code executes. If you put is outside, which is very much technically, possible, your code will not be executed (of course unless you intend to call the method from within the onCreate method.

Solution 5:

register the onClickListner for each button like.

button.setOnClickListener(this);

and override this method

@OverridepublicvoidonClick( View v )
{
     if ( v == button ) 
     {
            // your code...
     }

   ////  add others...
}

implement OnClickListener in your activity

publicclassyourActivityextendsActivityimplementsOnClickListener

Post a Comment for "Android: Why Can't I Call Setonclicklistener Method On A Button Outside Of An Oncreate Method?"