2011-09-13 66 views
-1

昨天我問了這個問題(http://stackoverflow.com/questions/7392321/how-do-you-disable-a-button-inside-of-an-alertdialog)並相應地修改了我的代碼。今天早上,我在模擬器中運行代碼並收到了NPE。下面是代碼:如何禁用AlertDialog中的按鈕?後續問題

public void monster() { 
     int playerint = settings.getPlayerInt(); 
     int monsterint = settings.getMonsterInt(); 



     AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
     alertbox.setMessage("You have Encountered a Monster"); 

     alertbox.setPositiveButton("Fight!", 
       new DialogInterface.OnClickListener() { 

        // do something when the button is clicked 
        public void onClick(DialogInterface arg0, int arg1) { 
         createMonster(); 
         fight(); 

        } 
       }); 

     alertbox.setNeutralButton("Try to Outwit", 
       new DialogInterface.OnClickListener() { 

        // do something when the button is clicked 
        public void onClick(DialogInterface arg0, int arg1) { 
         // This should not be static 
//      createTrivia(); 
         trivia(); 

        } 
       }); 

     // Return to Last Saved CheckPoint 
     alertbox.setNegativeButton("Run Away!", 
       new DialogInterface.OnClickListener() { 

        // do something when the button is clicked 
        public void onClick(DialogInterface arg0, int arg1) { 
//      runAway(); 
        } 
       }); 

這是那裏的問題就開始

// show the alert box 
      alertbox.show(); 
      AlertDialog dialog = alertbox.create(); 
      Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
      if(monsterint > playerint) { 
       button.setEnabled(false); 
      } 
     } 

任何人都知道我做錯了嗎?

回答

-1

我tryed這些代碼,並將其工作的,你需要隱藏首秀確定

AlertDialog.Builder alertbox = new AlertDialog.Builder(this); 
     alertbox.setMessage("You have Encountered a Monster"); 
     alertbox.setPositiveButton("asdasd", null); 

     alertbox.show(); 
     alertbox.setPositiveButton("", null); 
     alertbox.show(); 
+0

@Frank Bozzo我只是表示如何隱藏按鈕:(和我是錯誤的,但梅林是錯誤的'button.setVisibility(View.GONE);' – PedroAGSantos

+0

@FrankBozzo我已經更新了我的答案,並給出了完整的解釋,爲什麼會發生這種情況 – Merlin

2

編輯

起初我還以爲代碼應爲:

 AlertDialog dialog = alertbox.create(); 
     Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
     if(monsterint > playerint) { 
      button.setEnabled(false); 
     } 

     dialog.show(); 

我預計這會起作用,然而無論你如何烹飪這段代碼,調用getButton(int which)總是返回null。

這似乎沒有任何明智的理由。我很想說這是API中的一個錯誤。我的目標是爲API級別8

UPDATE

恭喜你已經發現Android Bug #6360見註釋#4的解決方法,

您也可以在可能的indirect duplicate of this question

看看,解決方法是在dialog.show()之後調用getButton:

 AlertDialog dialog = alertbox.create(); 

     dialog.show(); 

     Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
     if(monsterint > playerint) { 
      button.setEnabled(false); 
     } 
+0

實際上並非如此。直接在'AlertDialog.Builder'對象上調用show()會隱式調用它的create()方法,然後顯示結果。 – Devunwired

+0

感謝您指出...每天學習新內容我已更新我的回答 – Merlin

+0

代碼仍在生成NPE –

9

你有兩個問題。首先是你應該分別打電話show()create()。你實際上做了什麼是隱式創建一個AlertDialog並用alertbox.show()顯示它,然後在它的正下方創建第二個AlertDialog,用於操縱該按鈕。我們儘量保持對Builder的直接調用。

也和自己沒有真正得到,直到AlertDialog創建的按鈕是用於顯示(基本上,AlertDialog.show()被稱爲後...再次,不要被準備的更直接的原因,您的NPE崩潰,在AlertDialog土地與AlertDialog.Builder.show()方法混淆)。爲了您的目的使用AlertDialog,您需要在顯示對話框後獲取並操作按鈕狀態。下面是修復此最終代碼段的修改:

//Create the AlertDialog, and then show it 
AlertDialog dialog = alertbox.create(); 
dialog.show(); 
//Button is not null after dialog.show() 
Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
if(monsterint > playerint) { 
    button.setEnabled(false); 
} 

HTH

+0

+1用於定義對話框的生命狀態 – Merlin

+0

謝謝您的深入解答...不幸的是,我仍然得到即使在這些修改之後,NPE也會崩潰!有任何想法嗎? –

2

的技巧是,你需要使用AlertDialog.Builder.show()方法重新調校的AlertDialog對象。無需撥打AlertDialog.Builder.create()

實施例:

 AlertDialog dialog = alertbox.show(); 
     if(monsterint > playerint) { 
      Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
      button.setEnabled(false); 
     }