今天设置AlertDialog的按钮不可用,一直出现空指针异常,看来看去代码也挺正常啊,AlertDialog.Builder.create(),之后调用
- 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
- (AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
Before or after:
- dialog=builder.create()
原来必须在dialog.show()之后,才能用
- dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
否则即便是在create方法之后,获取到的Button仍然是空的,于是就看了一下API:
- 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.
按我的理解, 就是说在Show(Display)这个对话框之前,即便已经调用create方法,它的状态是不确定的,可以做任何处理,这时候就不能获取到对话框的Button及其他元素。
本文转自 breezy_yuan 51CTO博客,原文链接:http://blog.51cto.com/lbrant/429518,如需转载请自行联系原作者