2013-02-23 80 views
0

我正尝试使用项目选项创建AlertDialog。我使用下面的代码在Android中的AlertDialog中显示项目时出现异常

final CharSequence[] items={"One","two","three"}; 

alertDialog = new AlertDialog.Builder(this).create(); 
alertDialog.setTitle("Choose COlor");         
alertDialog.setItems(items, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // The 'which' argument contains the index position 
          // of the selected item 
         } 
       }); 

但我一直得到错误的编译

的方法setItems(CharSequence的[],新 DialogInterface.OnClickListener(){})是不确定的键入 AlertDialog

我很困惑,因为我看到使用该代码的所有例子,那么为什么这个错误?

回答

0

setItemsAlertDialog.Builder而不是AlertDialog的方法。

您应该在构建器上调用所有方法,而不是对话框。例如:

alertDialog = new AlertDialog.Builder(this) 
       .setTitle("Choose COlor")        
       .setItems(items, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // The 'which' argument contains the index position 
         // of the selected item 
        } 
       }) 
       .create(); 
+0

谢谢..你救了我的日子:-) – user669231 2013-02-23 04:11:40

相关问题