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

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

问题描述

荫刨给创建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.

推荐答案

看看这里的答案:<一href="http://$c$c.google.com/p/android/issues/detail?id=6360">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

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

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