0

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

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

4

3 回答 3

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.....
于 2012-06-28T17:59:44.897 回答
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();
于 2012-06-28T18:00:05.890 回答
0

在 if else 之前执行此操作。

AlertDialog.Builder alertDialogBuilder = null;

并更换你的

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

alertDialogBuilder = new AlertDialog.Builder(context);
于 2012-06-28T18:01:19.410 回答