今天设置AlertDialog的按钮不可用,一直出现空指针异常,看来看去代码也挺正常啊,AlertDialog.Builder.create(),之后调用

            
  1. dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);  

网上也找到相似的问题:http://www.anddev.org/set_positive_button_as_disabled_in_alertdialog-t13506.html

I get another trouble with it. I'm trying to retrieve this button on onCreateDialog(...) method, I always get nullPointerException by using

            
  1. (AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);   

Before or after:

            
  1. dialog=builder.create()  

 原来必须在dialog.show()之后,才能用

            
  1. dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);   

否则即便是在create方法之后,获取到的Button仍然是空的,于是就看了一下API:
 

                
  1. Creates a AlertDialog with the arguments supplied to this builder. It does not show() the dialog. This allows the user to do any extra processing before displaying the dialog. Use show() if you don't have any other processing to do and want this to be created and displayed.   
  2.  

按我的理解, 就是说在Show(Display)这个对话框之前,即便已经调用create方法,它的状态是不确定的,可以做任何处理,这时候就不能获取到对话框的Button及其他元素。

更多相关文章

  1. Android(安卓)TypeArray
  2. Android判断某一应用是否正在运行与判断某一Service是否正在运行
  3. android 记录和恢复ListView滚动的位置 四种方法
  4. android中EditText只允许输入汉字(过滤汉字)
  5. Android(安卓)5.0以上版本去掉Button自带阴影效果的方法
  6. android CTS测试使用方法
  7. Android(安卓)MediaPlayer类
  8. Android(安卓)WebView开发常见问题
  9. Android(安卓)启动分析-init进程&init.rc

随机推荐

  1. Android 获得本机ip地址和MAC地址
  2. 输入法弹出后,不影响activity原有布局(and
  3. android pdf 阅读器开发, pdf demo, pdf
  4. Android ListView设置Item选中状态
  5. Android Q中外部存储盘路径正则表达式的
  6. android 中调用接口发送短信
  7. 转载:Android(安卓)实用工具Hierarchy Vie
  8. Android(安卓)文件存储
  9. android学习资料链接汇总
  10. Android、JUnit深入浅出(七)——总…