我想要一个对话框,用户可以在其中输入用户名和密码来注册他的应用程序 显示对话框,但如果我输入任何数据并单击确认按钮,则会出现异常 =( 有人知道为什么我会得到这个异常吗?
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_register_app, (ViewGroup) findViewById(R.id.layout_root));
//Start building dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.dialog_register_app_title));
builder.setView(layout);
builder.setCancelable(false);
builder.setPositiveButton(getString(R.string.dialog_register_confirm_button_text), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Auslesen der EditText
EditText oUsername = (EditText) findViewById(R.id.edit_username);
String strUsername = oUsername.getText().toString(); // This is line where exception is calling
//Do something with strUsername
}
});
AlertDialog alert = builder.create();
alert.show();
...
...
...
最佳答案
这应该可以使它工作,因为应用程序需要知道它需要从哪个布局(View)中选择带有 id edit_username 的 edittext....因此应该在您创建的 View 上调用 findViewById 方法,而不是在查看当前 Activity ...希望有帮助
EditText oUsername = (EditText) layout.findViewById(R.id.edit_username);
String strUsername = oUsername.getText().toString(); // This is line where exception is calling
...
关于Android 对话框调用目标异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371968/