Code in Beginning Android Games book giving error
I'm a newbie to android development and i was reading the code for creating a custom button which tells u the no of times u clicked tht button. The code is as follows:
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HelloWorldActivity extends Activity implements View.OnClickListener {
Button button;
开发者_如何学编程 int touchCount;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button=new Button(this);
button.setText("Click Me");
button.setOnClickListener(this);
setContentView(button);
}
public void onCLick(View v)
{
touchCount++;
button.setText("touched me"+touchCount+"time(s)");
}
}
THE ERROR :
The type HelloWorldActivity must implement the inherited abstract method View.OnClickListener.onClick(View) - Breakpoint:HelloWorldActivity
I looked in a couple of sites and tried something but nothing worked. Please help me out. Thank you.
It should be onClick
, not onCLick
(lower case l).
Your HelloWorldActivity is acting as (or implementing) a View.OnClickListener
. As an OnClickListener Java is expecting you implement the method "onClick". Java is case sensitive and interprets onClick
is different from onCLick
(where you have a capital L rather then the expected lower case). Try updating your onClick to a lower l.
精彩评论