2011-01-05 17 views
33

Iam planing para dar crear 3 botones con layout_weight = 1, no está interesado en el diálogo personalizado. Por lo tanto, he escrito el código a continuación. No funciona. Siempre el botón sí me da nulo . ¿Qué hay de malo en este código?método de alertaDialog.getButton() da excepción de puntero nulo 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(); 

Saludos, Desarrollador de Android.

Respuesta

52

Echa un vistazo aquí la respuesta: http://code.google.com/p/android/issues/detail?id=6360

Como se dice en el comentario # 4 debe llamar show() en su diálogo antes de poder acceder a los botones, no se dispone de antemano. Para una solución automática sobre cómo modificar los botones tan pronto como estén listos ver Mickeys answer

+1

Aún problema persist.No uso de ese enlace. – ADIT

+11

Lea el comentario # 4, use dialog.show(); antes de usar getButton() – vieux

+1

Gracias wieux.It está trabajando – ADIT

4

Gracias wieux.But para los nuevos desarrolladores entender el propósito Iam código de re-escritura de abajo

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

no funciona para mí. –

43

Esto funciona para mí:

Sólo
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(); 

en este orden, llame alertDialog.setOnShowListener después create()

+0

setOnShowListener es API 8+ –

+1

setOnShowListener resolvió mi problema –

+0

Esto debe marcarse como la respuesta correcta. +1 –

Cuestiones relacionadas