【问题标题】:"void is an invalid type for the variable buttonOK" - trying to close dialog after clicking button“void 是变量 buttonOK 的无效类型” - 单击按钮后尝试关闭对话框
【发布时间】:2012-09-27 17:14:44
【问题描述】:

前段时间我做了一个简单的对话框。一切看起来都很好,但是在尝试关闭它后我遇到了麻烦。错误是 "void is an invalid type for the variable buttonOK"

嗯,最好给个截图链接: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();
}

我应该怎么做才能让它工作?

PS 我在public void buttonOK(View view) 有错误,具体在view - Duplicate local variable view 我应该重命名它吗,例如view2

好的,我找到了解决方案。

问题是(注意到 Ridcully)在另一个方法 aboutApp() 中定义了方法 buttonOK(),这是在 java 中无法完成的(uach,现在我知道了:D)。

我只是替换了代码:@Override public void buttonOK(View view) { 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();
                    }
                });

现在可以了,谢谢大家的帮助!

【问题讨论】:

  • 你想调用哪个按钮buttonOK()?默认对话框按钮之一还是您在aboutapp.xml 中定义的按钮?
  • emmm,这个定义在aboutapp.xml中
  • 您是否尝试使用android:onClick="buttonOK" 属性?

标签: java android android-button


【解决方案1】:

您在另一个方法 (aboutApp()) 中定义了一个方法 (buttonOK())。这在 Java 中是不可能的。编译器试图理解这一点,并假设buttonOk 是一个变量——因此是误导性的错误消息。

【讨论】:

【解决方案2】:

您错误地使用对话框。这是一个给你的例子。

AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        //TODO Handle click here

    }
});

dialog.show();

【讨论】:

    【解决方案3】:

    由于您尝试将android:onClick 属性与"buttonOK" 一起使用,您必须public void buttonOK(View v) {} 声明为类中的常规方法。 (目前您正在尝试将其嵌套在 aboutApp() 中,同时也了解您不能在 Java 中嵌套方法。)

    public class MyActivity extends Activity {
        Dialog dialog;
    
        public void onCreate(Bundle savedInstanceState) { ... }
    
        public void aboutApp(View view) {
            // custom dialog
            dialog = new Dialog(context);
            ...
        }
    
        // Move your method here:
        public void buttonOK(View v) {
            dialog.dismiss();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      相关资源
      最近更新 更多