Google Android“HelloFormStuff”教程出现Java错误

我是一名java新手.我按照http://developer.android.com/resources/tutorials/views/hello-formstuff.html中的教程添加一个按钮和OnClick处理程序,将教程代码复制到我的:

public class FormStuff 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.android_button);
        button.setonClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(FormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });
        }
}

在Eclipse中,这会产生两个错误

Description Resource    Path    Location    Type
The method setonClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})  FormStuff.java  /FormStuffExample/src/com/example/formstuffexample  line 17 Java Problem
The type new DialogInterface.OnClickListener(){} must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int) FormStuff.java  /FormStuffExample/src/com/example/formstuffexample  line 17 Java Problem

我究竟做错了什么?谢谢!

解决方法:

完全基于错误消息……

您正在使用(隐式)错误的OnClickListener接口/类.看起来有两个,View.OnClickListener和DialogInterface.OnClickListener.

解决方案是完全限定您的匿名OnClickListener.

button.setonClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(FormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
            }
        });

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...