Ale*_*r W 10 java layout android android-edittext android-alertdialog
该主题解释了我正在追求的内容......我无法从android中的自定义视图中检索EditText.我得到的只是一个Nullpointer异常.:/我已经用注释标记了代码中的问题.ID:s是正确的,我的XML布局是一个包含两个EditText属性的简单RelativeLayout.显然我在这里遗漏了一些微不足道的东西,但我现在已经盯着代码差不多2个小时而没有解决这个问题,所以我想我会试一试.
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
建议后,以下代码是一个工作的代码!再次感谢!
protected void showLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.activity_login, null))
// Add action buttons
.setPositiveButton(R.string.login, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Dialog f = (Dialog) dialog;
/* ERROR HERE! */
EditText uName, passWord;
uName = (EditText) f.findViewById(R.id.login_username);
passWord = (EditText) f.findViewById(R.id.login_password);
Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString());
/* STOP */
if(the_view.getSocketTask().isConnected) {
the_view.getSocketTask().send_command("LOGIN ");
} else {
showToast("Not connected!");
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.create().show();
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!亚历克斯
Moh*_*lah 18
将对话框转换为视图:
View v_iew=inflater.inflate(R.layout.activity_login, null)) ;
builder.setView(v_iew);
Run Code Online (Sandbox Code Playgroud)
然后替换:
uName = (EditText) findViewById(R.id.login_username);
passWord = (EditText) findViewById(R.id.login_password);
Run Code Online (Sandbox Code Playgroud)
同
uName = (EditText) v_iew.findViewById(R.id.login_username);
passWord = (EditText) v_iew.findViewById(R.id.login_password);
Run Code Online (Sandbox Code Playgroud)