2013-02-23 79 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

相關問題