alertDialog.getButton() 方法给出空指针异常 android [英] alertDialog.getButton() method gives null pointer exception android

查看:33
本文介绍了alertDialog.getButton() 方法给出空指针异常 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算用 layout_weight=1 创建 3 个按钮,对自定义对话框不感兴趣.所以我写了下面的代码.它不起作用.总是是按钮给我空.这段代码有什么问题?

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://code.google.com/p/android/issues/detail?id=6360

正如评论 #4 中所说,您必须在对话框中调用 show() 才能访问按钮,它们事先不可用.有关如何在按钮准备好后立即修改按钮的自动解决方案,请参阅 Mickeys 回答

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

这篇关于alertDialog.getButton() 方法给出空指针异常 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆