且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AlertDialog中的EditText始终为null

更新时间:2023-01-28 08:59:16

请注意如何在匿名内部类中解决所有方法调用.

Take note of how all of your method calls are being resolved within your anonymous inner classes.

findViewById是一种存在于视图和活动上的方法.活动上此方法的版本在活动窗口的视图层次结构内搜索视图.视图上的版本会搜索该视图实例和所有附加的子代.

findViewById is a method that exists on views and on your activity. The version of this method on your activity searches for a view within the activity window's view hierarchy. The version on views searches that view instance and all attached children.

您对有问题的代码行的调用:

Your call on the problematic line of code:

EditText txtAccName = (EditText) findViewById(R.id.txtEditName);

正在解析为Activity#findViewById.但是对话框的布局未附加到活动窗口,而是已附加到对话框.您可以通过几种方式找到正确的视图引用,但最简单的情况可能是从夸大的布局的根目录中进行搜索:

is resolving to Activity#findViewById. But your dialog's layout is not attached to your activity window, it's attached to the dialog. You can find the correct view reference in several ways but the simplest in your case is probably to search from the root of the layout that you inflated:

EditText txtAccName = (EditText) layout.findViewById(R.id.txtEditName);