I am unable to implement the setOnClickListener() for my button
-
03-12-2019 - |
Frage
I am trying to create an SQL database in Android. I am currently following an online tutorial but every time I try to add an OnClickListener for my button I get the following error message...
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments(SqlExample).
In the quick fix eclipse allows me to implement an OnClickListener however I end up with the following import "public class SqlExample extends Activity implements OnClickListener, android.view.View.OnClickListener".... Is this correct? It is something I have never seen before.
Lösung
Add thiss line import android.view.View.OnClickListener;
to the import section.
private final OnClickListener yourButtonClickListener = new OnClickListener()
{
@Override
public void onClick(final View v)
{
//Your code
}
};
Andere Tipps
Change your code according to this sort of pattern.
// NAME OF THE ANDROID COMPONENT. REPLACE WITH BUTTON
Button mybutton = (Button) findViewById(R.id.mybtnid);
mybutton.setOnClickListener(new View.setOnClickListener() { public void onClick(View v) {
// PLACE YOUR CLICK CODE HERE..
}
});
This works!
In Eclipse, do Ctrl+Shift+O for any such things and all imports will be automatically added.