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

2023-09-12 07:49:22 作者:廢話販賣机

荫刨给创建3个按钮与layout_weight = 1,没兴趣定制dialog.So我已经写了下面code.It不working.Always是按钮,让我空。 什么是错在这个code?

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. Whats wrong in this code?

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

Regards, Android developer.

推荐答案

看看这里的答案:http://$c$c.google.com/p/android/issues/detail?id=6360

因为它说,在评论#4你必须调用显示()的对话框,然后才能访问按钮,它们是不可用事先。有关如何,尽快为他们准备好看到米奇回答修改按钮自动解决方案

As it says in comment #4 you must call show() on your dialog before you can access the buttons, they are not available beforehand. For an automatic solution on how to modify the buttons as soon as they are ready see Mickeys answer

 
精彩推荐