alertDialog.getButton() method gives null pointer exception android

后端 未结 3 571
借酒劲吻你
借酒劲吻你 2020-11-27 17:20

Iam planing to give create 3 buttons with layout_weight=1, not interested in custom dialog.So I have written below code.It is not working.Always yes button gives me null. Wh

3条回答
  •  孤城傲影
    2020-11-27 17:48

    Thanks wieux. But for new developers understanding purpose I am re-writing code below

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

提交回复
热议问题