2011-11-07 66 views
3

我正嘗試使用單選項目(單選按鈕)列表創建一個警報對話框。當用戶單擊兩個選項菜單中的一個時,將會調用此對話框。選項菜單中的AlertDialog

不幸的是,我無法獲得此代碼的工作。當我點擊第一個選項菜單(設置)時,什麼都沒有出現。但是當我點擊第二個選項菜單時,烤麪包就會出現。如果有人能指出明顯的錯誤,我將不勝感激。

以下是代碼的警告對話框中和選項菜單中列出:

import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 

ProgressDialog msgInitGPS = null; 
final CharSequence[] items = {"m/s", "km/h", "mph"}; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.settings: AlertDialog.Builder UnitSelection = new AlertDialog.Builder(this); 
          UnitSelection.setTitle("Select Unit"); 
          UnitSelection.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int item) { 
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 
           } 
          }); 
          AlertDialog alert = UnitSelection.create(); 
          break; 

     case R.id.help:  Toast.makeText(this, "This will launch the Help screen", Toast.LENGTH_LONG).show(); 
          break; 
    } 
    return true; 
} 

非常感謝您的時間和幫助!

+1

我想你錯過了'alert.show()'。或者,'UnitSelection.show()'應該創建並顯示這個對話框。 – harism

+0

非常感謝你,它的工作! – Ganesh

回答

5

不要忘記這一點:

alert.show(); 
+0

謝謝大家,它的工作! :) – Ganesh