2011-01-05 28 views
33

Iam bào tạo cho tạo 3 nút với layout_weight = 1, không quan tâm đến hộp thoại tùy chỉnh. Vì vậy, tôi đã viết dưới đây code.It không hoạt động. Luôn luôn có nút mang lại cho tôi null . Có gì sai trong mã này?alertDialog.getButton() phương pháp cung cấp cho con trỏ ngoại lệ android 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(); 

Kính trọng, Nhà phát triển Android.

Trả lời

52

Hãy xem ở đây cho câu trả lời: http://code.google.com/p/android/issues/detail?id=6360

Vì nó nói trong comment # 4 bạn phải gọi show() trên hộp thoại của bạn trước khi bạn có thể truy cập vào các nút, họ không có sẵn trước đó. Đối với một giải pháp tự động về cách sửa đổi các nút ngay sau khi họ đã sẵn sàng nhìn thấy Mickeys answer

+1

Tuy nhiên vấn đề persist.No Sử dụng liên kết đó. – ADIT

+11

Vui lòng đọc chú thích # 4, sử dụng dialog.show(); trước khi sử dụng getButton() – vieux

+1

Cảm ơn wieux.It đang hoạt động – ADIT

4

Cảm ơn wieux.But cho nhà phát triển mới hiểu mục đích Iam đang viết lại dưới

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

nó không hoạt động đối với tôi. –

43

này làm việc cho tôi:

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(); 

chỉ theo thứ tự này, hãy gọi alertDialog.setOnShowListener sau create()

+0

setOnShowListener là API 8+ –

+1

setOnShowListener đã giải quyết được vấn đề của tôi –

+0

Điều này sẽ được đánh dấu là câu trả lời đúng. +1 –

Các vấn đề liên quan