2011-01-05 57 views
33

Iam刨給創建3個按鈕與layout_weight = 1,不感興趣的自定義dialog.So我寫下面的code.It不工作。總是是按鈕給我null 。 這段代碼有什麼問題?alertDialog.getButton()方法給出空指針異常android

AlertDialog dialog= new AlertDialog.Builder(this).create(); 
      dialog.setIcon(R.drawable.alert_icon); 
      dialog.setTitle("title"); 
      dialog.setMessage("Message"); 
      dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
               } 
      }); 
      Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 
      Log.w("Button",""+yesButton);//here getting null 
      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f); 
      yesButton.setLayoutParams(layoutParams); 
      dialog.show(); 

關注, Android開發人員。

回答

52

看看這裏的答案:http://code.google.com/p/android/issues/detail?id=6360

因爲它說,在評論#4你必須調用show()你的對話框上,然後才能訪問按鈕,它們是不可用事先。有關如何儘快爲他們準備好看到Mickeys answer

+1

不過問題persist.No使用呼叫alertDialog.setOnShowListener。 – ADIT 2011-01-05 13:09:53

+11

請閱讀註釋#4,使用dialog.show();之前使用getButton() – vieux 2011-01-05 13:18:31

+1

謝謝wieux.It工作 – ADIT 2011-01-05 13:46:38

4

感謝wieux.But新開發者理解下面

AlertDialog dialog= new AlertDialog.Builder(this).create();    
dialog.setIcon(R.drawable.alert_icon);    
dialog.setTitle("title");    
dialog.setMessage("Message");    
dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {     @Override     
public void onClick(DialogInterface arg0, int arg1) {             
}    
}); 
    dialog.show(); 
       Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);    Log.w("Button",""+yesButton);//here getting null    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);    yesButton.setLayoutParams(layoutParams);   
+1

它不適合我。 – 2013-05-10 14:47:45

43

爲此蔭重新編寫代碼修改按鈕自動解決方案適用於我:

只有
AlertDialog alertDialog = new AlertDialog.Builder(this) 
       .setMessage(message) 
       .setCancelable(true) 
       .setPositiveButton("Yes", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
          //do smthng 
         }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //do snthn 
        } 
       }).create(); 

     alertDialog.setOnShowListener(new OnShowListener() { 
      @Override 
      public void onShow(DialogInterface dialog) {     // 
       Button positiveButton = ((AlertDialog) dialog) 
         .getButton(AlertDialog.BUTTON_POSITIVE); 
       positiveButton.setBackgroundDrawable(getResources() 
         .getDrawable(R.drawable.btn_default_holo_dark)); 

       Button negativeButton = ((AlertDialog) dialog) 
         .getButton(AlertDialog.BUTTON_NEGATIVE); 
       positiveButton.setBackgroundDrawable(getResources() 
         .getDrawable(R.drawable.btn_default_holo_dark)); 
      } 
     }); 

     alertDialog.show(); 

順序,之後該鏈接的create()

+0

setOnShowListener是API 8+ – 2014-06-24 02:45:00

+1

setOnShowListener解決了我的問題 – 2015-06-16 01:35:29

+0

這應該被標記爲正確的答案。 +1 – 2017-03-09 23:31:47