为什么我的EditText资源在我的Android应用程序中评估为null?

wfb*_*ale 1 android dialog android-edittext

所以我有一个接受用户名和密码的对话框,但是当我尝试从EditText对象获取用户名和密码时,我似乎无法EditText从资源中获取对象,我的代码看起来像这样.

protected Dialog onCreateDialog(int ID){
     LayoutInflater factory = LayoutInflater.from(this);
     final View textEntryView = factory.inflate(R.layout.login_dialogue, null);
     return new AlertDialog.Builder(this)
         .setTitle("Login:")
         .setView(textEntryView)
         .setPositiveButton("login", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {
                 /* Problem occurs below */
                 View aView = findViewById(R.id.login_dialogue_username);
                 String username = ((EditText) aView).getText().toString();
                 String password = ((TextView) findViewById(R.id.login_dialogue_password)).getText().toString();
                 boolean success = attemptLogin(username, password);
             }
         })
         .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int whichButton) {

                 /* User clicked cancel so do some stuff */
             }
         })
         .create();
 }
Run Code Online (Sandbox Code Playgroud)

因此,当我在调试中单步执行时,aView变量为null,但我看不出原因.资源在我的R文件中,在eclipse中显示为蓝色,表示它与R文件的名称匹配.有谁知道这里发生了什么?

Cra*_*igy 5

您正在查看活动的布局.尝试使用

textEntryView.findViewById(R.id.login_dialogue_username);
Run Code Online (Sandbox Code Playgroud)

代替.