Below is my program and I am getting this error:
Description Resource Path Location Type
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (welcome) welcome.java /testcalculator/src/com/testcalculator line 31 Java Problem
welcome.java
package com.testcalculator;
public class welcome extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome1);
Button playBtn = (Button) findViewById(R.id.playBtn);
playBtn.setOnItemClickListener();
Button exitBtn = (Button) findViewById(R.id.exitBtn);
exitBtn.setOnClickListener(this);
}
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.playBtn :
i = new Intent(this, testcalculator.class);
startActivity(i);
break;
case R.id.exitBtn :
finish();
break;
}
}
}
The problem is, I have all the required import options in my problem but still I am getting error message.
You are passing your Activity class as the OnClickListener in this line:
However, your class needs to explicitly declare that it is implementing the
View.OnCLickListenerinterface. Change your class declaration line to this:A couple of other things to note:
You wrote
playBtn.setOnItemClickListener(). Perhaps you meantplayBtn.setOnClickListener(this)? Buttons don’t have OnItemClickListenersYou can also set an OnClickListener without having the activity class itself implement the interface by declaring an anonymous class. Like this:
This way is used more often because it is more readable. By segregating the button onClick code, you can easily tell which button does what, as opposed to putting it all into one method and having the class itself implement OnClickListener.