Google Android"HelloFormStuff"教程出现Java错误

pns*_*wdv 3 android

我是一名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();
            }
        });
        }
}
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

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

Kev*_*ose 9

完全基于错误消息......

您正在使用(隐式)错误的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();
            }
        });
Run Code Online (Sandbox Code Playgroud)