This makes no sense, I am given an error saying that setOnClickListener can't be applied to MainActivity. But i've made other projects where I've never encountered this problem. What's going on?
public class MainActivity extends ActionBarActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = ((Button) findViewById(R.id.button));
button.setOnClickListener(this);
}
You have to declare MainActivity like this:
public class MainActivity extends ActionBarActivity implements View.OnClickListener
and after that you have to override the onClick method in MainActivity
@Override
public void onClick(View v) {
//do something...
}
As we can see you havn't implemented the View.OnClickListener
on Main activity..
this
will be used as MainActivity
but the parameter that can be passed is OnClickListener
SideNote: Always try to typecast before you use, this greatly reduce the complications and sometimes takes you to mistakes you are doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With