问题描述
前一段时间,我做了一个简单的对话框.一切看起来都很好,但是在尝试关闭它之后我遇到了麻烦.错误是"void是变量buttonOK的无效类型"
.
Some time ago i've made a simple dialog. Everything looks fine, but i'm meeting troubles after trying to close it. The error is"void is an invalid type for the variable buttonOK"
.
嗯,更好的是,我将提供指向屏幕快照的链接: http://i.imgur.com/tiAiI.png
Hmm, better i'll give a link to screenshot: http://i.imgur.com/tiAiI.png
对话框代码:
public void aboutApp(View view) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.aboutapp);
dialog.setTitle("about ");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("bla bla bla ");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
@Override
public void buttonOK(View view) {
dialog.dismiss();
}
dialog.show();
}
我应该怎么做才能使其正常工作?
What should I do to make it works?
PS我在 public void buttonOK(查看视图)
处出错,在 view
处过错-重复的局部变量视图
我应该重命名例如 view2
?
PS I've got error at public void buttonOK(View view)
, extacly at view
- Duplicate local variable view
Should I rename it in, for example view2
?
问题是(注意到 Ridcully )在另一个方法 aboutApp()
中定义了方法 buttonOK()
用Java完成(嗯,现在我知道了:D).
The problem was (as noticed Ridcully) that defined method buttonOK()
in another method aboutApp()
, what can't be done in java (uach, now I know it :D).
我只是替换了代码: @Overridepublic void buttonOK(查看视图){dialog.dismiss();}
收件人:
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View view2) {
dialog.dismiss();
}
});
现在可以正常工作了,谢谢大家的帮助!
Now it works, thx all for help!
推荐答案
您已在另一个方法( aboutApp()
)中定义了一个方法( buttonOK()
).在Java中这是不可能的.编译器试图弄清这一点,并假设 buttonOk
一定是一个变量,从而误导了错误消息.
You have defined a method (buttonOK()
) within another method (aboutApp()
). This is not possible in Java. The compiler tries to make sense of this and supposes somhow that buttonOk
is meant as a variable -- thus the misleading error message.
这篇关于“对于变量buttonOK,void是无效类型".-尝试在单击按钮后关闭对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!