OnClickListener 无法解析为类型(Eclipse)
你好,我是编程新手,我正在尝试构建我的第一个简单应用程序,我希望在按下 ImageButton 时播放一个简短的声音剪辑。
在输入我的代码时,我收到语句错误;
Button.setOnClickListener(new OnClickListener() {
单击侦听器带有下划线,当我出现错误时,Eclipse 告诉我 OnClickListener 无法解析为类型。
这是我的代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.view;
import android.view.view.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton Button = (ImageButton) findViewById(R.id.imageButton1);
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
我读到一条建议,说要添加;
import android.view.view;
以及
import android.view.view.OnClickListener;
这些导入语句也被突出显示。 这些错误是否是由我的计算机上的 eclipse 设置方式引起的?
任何帮助将不胜感激
Hello im new to programming, im trying to construct my first simple application, im looking to play a short soundclip on the push of an ImageButton.
while typing out my code i get an error with the statement;
Button.setOnClickListener(new OnClickListener() {
The on click listener is underlined and when i go to the error eclipse tells me that OnClickListener cannot be resolved to a type.
Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.view;
import android.view.view.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageButton Button = (ImageButton) findViewById(R.id.imageButton1);
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
I read a suggestion that said to add;
import android.view.view;
aswell as
import android.view.view.OnClickListener;
These import statements are also highlighted.
Could these errors be caused by how eclipse is set up on my computer?
Any help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于初学者来说,当您看到导入错误时,最好让 Eclipse 管理所有导入,方法是点击 Ctrl+Shift+O。
看来您的问题是由于:
应该是:
android.view.View.OnClickListener 也是如此。
如果删除手动添加的两行并按 Ctrl+Shift+O,一切都会自行修复。
For starters, it's always best to let Eclipse manage all imports by tapping Ctrl+Shift+O when you see an import error.
It seems that your problem is due to:
Which should be:
Same goes with android.view.View.OnClickListener.
If you remove the two lines you've manually added and hit Ctrl+Shift+O, everything should fix itself.
添加
到您的
import
部分,它应该可以工作。Add
to your
import
section and it should work.import 语句中的第二个“视图”是一个类(因此,
OnClickListener
是一个内部类/接口)并且应该大写:The second "view" in the import statement is a class (hence,
OnClickListener
is an inner class/interface) and should be capitalised:确保你的类实现了 OnClickListener
make sure your class implements OnClickListener
如果你仍然有错误,你可以像这样使类抽象
公共抽象类 MainActivity 扩展 Activity 实现 OnClickListener {
if you still have error you can make the class abstract like this
public abstract class MainActivity extends Activity implements OnClickListener {
如果您使用新的 Android Studio,则必须声明新的 OnClickListener
作为 View.OnClickListener。否则 Android Studio 会感到困惑并且无法理解。
If you are using the new Android Studio you must declare your new OnClickListener
as View.OnClickListener. Otherwise Android Studio gets confused and won't understand.