为什么这个变量“不能解决”?

时间:2012-06-28 17:55:58

标签: android dialog android-alertdialog

我有两个单选按钮作为一个单选按钮和一个“执行”按钮 - 所以你选择单选按钮,点击“执行”,它根据无线电选择发出备用对话框。我在下面的第二行(创建警告对话框构建器)中收到错误:

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无法解决”,我不知道为什么。

3 个答案:

答案 0 :(得分:0)

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.....

答案 1 :(得分:0)

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

像这样:

    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();

答案 2 :(得分:0)

在你的if之前做这件事。

AlertDialog.Builder alertDialogBuilder = null;

并替换你的

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

alertDialogBuilder = new AlertDialog.Builder(context);