OnClickListener 的问题
嘿,我是 Android 新手,我只想处理点击事件,但我遇到了问题...这是我的代码:
package karim.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity implements android.view.View.OnClickListener {
private Button b1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) this.findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
@Override
public void onClick(View v) {
}
我收到了这个错误:
Description Resource Path Location Type
The method onClick(View) of type TestActivity must override a superclass method TestActivity.java /Test/src/karim/test line 33 Java Problem
你能告诉我出了什么问题吗???
请具体说明!!
Hey am new to android , i just want to handle a click event , but i got problems ...thi sis my code:
package karim.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class TestActivity extends Activity implements android.view.View.OnClickListener {
private Button b1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) this.findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
@Override
public void onClick(View v) {
}
and i got this error :
Description Resource Path Location Type
The method onClick(View) of type TestActivity must override a superclass method TestActivity.java /Test/src/karim/test line 33 Java Problem
can you please tell me whats wrong ????
Please Be specific !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的猜测是您正在使用 Java 5 编译器(或 IDE 中的 Java 5 编译器设置)进行编译。在 Java 5 中,
@Override
只能用于重写类方法的方法,而不能用于接口。在 Java 6 中扩展为接口方法重写。更改编译器版本,或删除
onClick
方法上的@Override
注解。My guess is that you're compiling with a Java 5 compiler (or Java 5 compiler settings in the IDE). In Java 5,
@Override
was only usable on a method overriding a method of a class, not an interface. It was extended to interface method overriding in Java 6.Change the compiler version, or remove the
@Override
annotation on theonClick
method.您不需要让您的 Activity 类实现 OnClickListener。摆脱它。您只需定义侦听器并将其分配给您的按钮即可。查看官方 android 文档:
http://developer.android。 com/guide/topics/ui/ui-events.html
You don't need to have your Activity class implement OnClickListener. Get rid of that. You just need to define the listener and assign it to your button. Take a look at the official android docs:
http://developer.android.com/guide/topics/ui/ui-events.html
或者在你的情况下
or in your case
作为“JB Nizet”答案的扩展:
处理
onClick
事件的最简洁方法是(在我看来)添加onClick
-attribute 到 XML-Layout 定义。在您的 java 代码中:
这样,您可以为每个
onClick
事件定义一个方法。从 API 级别 4 开始可用,对应 Android 1.6As an extension to "JB Nizet" answer:
The neatest way to handle
onClick
-events is (in my opinion) to add theonClick
-attribute to the XML-Layout definition.In your java-code:
This way, you can define a single method for every single
onClick
-event. This is available since API-Level 4 which corresponds to Android 1.6看起来您正在 eclipse 中的项目中使用低于 java 6 的编译器。右键单击您的项目,转到属性--> Java 编译器。确保您在编译器合规性字段中选择了 1.6。
Looks like you are using compiler below java 6 for your project in eclipse. Right click on your project, go to properties-->Java compiler. Make sure you have 1.6 selected in compiler compliance field.