gpt4 book ai didi

android - Google Android "HelloFormStuff"教程的 Java 错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:05 25 4
gpt4 key购买 nike

我是一个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 接口(interface)/类。看起来有两个,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 - Google Android "HelloFormStuff"教程的 Java 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1960551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com