【问题标题】:Why can this variable "not be resolved"?为什么这个变量“无法解析”?
【发布时间】:2012-06-28 17:55:57
【问题描述】:

我有两个单选按钮作为单选组,还有一个“执行”按钮 - 所以您选择单选按钮,点击“执行”,它会根据单选选项显示备用对话框。我在倒数第二行(创建警报对话框生成器)中收到以下错误:

private OnClickListener myClickcalcHandler = new OnClickListener() {
    public void myClickcalcHandler(View view) {
        switch (view.getId()) {
        case R.id.calcbutton:
            RadioButton insideButton = (RadioButton) findViewById(R.id.radioButton1);
            RadioButton outsideButton = (RadioButton) findViewById(R.id.radioButton1);
            }
        if
        (outsideButton.isChecked()){
            //do what you want
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);
                // set title
                alertDialogBuilder.setTitle("some outside activity");
                button = (Button) findViewById(R.id.emailbutton);
                // set dialog message
                alertDialogBuilder
                    .setMessage(R.string.email_long)
                    .setCancelable(false)
                    .setNegativeButton("Close",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });
        }
        else if
        (insideButton.isChecked()){
            //do what you want 
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);
            // set title
            alertDialogBuilder.setTitle("some inside activity");
            button = (Button) findViewById(R.id.emailbutton);
            // set dialog message
            alertDialogBuilder
                    .setMessage(R.string.email_long)
                    .setCancelable(false)
                    .setNegativeButton("Close",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });
        }
            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();
            // show it
        alertDialog.show();

            }

所以eclipse编辑器只是说“alertDialogBu​​ilder无法解决”,我不知道为什么。

【问题讨论】:

    标签: android dialog android-alertdialog


    【解决方案1】:

    decalare AlertDialog 全局表示在 if/else 块之外:

    private OnClickListener myClickcalcHandler = new OnClickListener() {
        public void myClickcalcHandler(View view) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            context);
    
            switch (view.getId()) {
            case R.id.calcbutton:
                RadioButton insideButton = (RadioButton) findViewById(R.id.radioButton1);
                RadioButton outsideButton = (RadioButton) findViewById(R.id.radioButton1);
                }
             //YOUR CODE HERE.....
    

    【讨论】:

    • 嗨 - 感谢您的及时回复 - 我已经尝试了所有这些,但仍然在 // 创建警报对话框 AlertDialog alertDialog = alertDialogBu​​ilder.create(); - 不确定我之前在复制部分上方的脚本中使用 alertDialogBu​​ilder 是否重要,但我假设我可以多次使用它?
    【解决方案2】:

    您需要在if/else 构造之外声明AlertDialog.Builder alertDialogBuilder,然后在其中定义它。

    像这样:

        AlertDialog.Builder alertDialogBuilder;
        if
        (outsideButton.isChecked()){
            //do what you want
            alertDialogBuilder = new AlertDialog.Builder(
                        context);
            // ...
        }
        else if
        (insideButton.isChecked()){
            //do what you want 
            alertDialogBuilder = new AlertDialog.Builder(
                    context);
            // ...
        }
            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();
            // show it
        alertDialog.show();
    

    【讨论】:

      【解决方案3】:

      在 if else 之前执行此操作。

      AlertDialog.Builder alertDialogBuilder = null;
      

      替换你的

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
      

      alertDialogBuilder = new AlertDialog.Builder(context);
      

      【讨论】:

      • 嗨 - 感谢您的及时回复 - 我已经尝试了所有这些,但仍然在 // 创建警报对话框 AlertDialog alertDialog = alertDialogBu​​ilder.create(); - 不确定我在复制部分上方的脚本中早些时候使用了 alertDialogBu​​ilder 是否重要,但我假设我可以多次使用它? ——
      • 如果您仍然遇到问题,请发布您正在使用的新代码,以及包含错误的行以及错误是什么。
      猜你喜欢
      • 2017-02-04
      • 2021-09-04
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 2014-11-22
      • 2012-04-14
      相关资源
      最近更新 更多