2012-06-28 35 views
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解决不了的”,我不知道为什么。

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

回答

0

decalare AlertDialog全球手段之外。

像这样:

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

嗨 - 感谢您的及时答复 - 我已经在//尝试所有这些,仍然得到了“解决不了”创建警报对话框 \t \t \t AlertDialog alertDialog = alertDialogBu​​ilder.create(); - 不确定是否重要,我已经在复制部分上面的脚本中使用了alertDialogBu​​ilder,但我认为我可以多次使用它? –

0

您需要声明AlertDialog.Builder alertDialogBuilderif/else构建的,然后在其中定义它:if/else语句块作为

0

在此之前,如果你还有。

AlertDialog.Builder alertDialogBuilder = null; 

并更换

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

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

嗨 - 感谢您的快速答复 - 我已经尝试过这些,仍然得到“无法解决”在/ /创建警报对话框AlertDialog alertDialog = alertDialogBu​​ilder.create(); - 不确定是否重要,我已经在复制部分上面的脚本中使用了alertDialogBu​​ilder,但我认为我可以多次使用它? - –

+0

如果您仍然遇到问题,请发布您正在使用的新代码,以及包含错误和错误的行。 – Eric

相关问题