alertDialog.getButton()方法给出了空指针异常android

时间:2011-01-05 12:35:02

标签: android alertdialog

我计划给出创建3个按钮,layout_weight = 1,对自定义对话框不感兴趣。所以我写了下面的代码。它不工作。总是是按钮给我null。 这段代码怎么了?

  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开发者。

3 个答案:

答案 0 :(得分:66)

在这里查看答案:http://code.google.com/p/android/issues/detail?id=6360

正如评论#4中所述,您必须先在对话框上调用show()才能访问按钮,但事先无法使用它们。有关如何在按钮准备就绪后立即修改按钮的自动解决方案,请参阅Mickeys answer

答案 1 :(得分:55)

这对我有用:

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

仅按此顺序,在alertDialog.setOnShowListener

之后致电create()

答案 2 :(得分:4)

谢谢wieux。但是对于新开发人员的理解目的,我正在重写代码

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