尝试在空对象引用上调用虚拟方法android.widget.EditText.getText()
试图从对话框内的EditText获取Edit Text的值,但一次又一次地出现此错误 尝试调用虚拟方法“android.text.Editable” 空对象引用上的android.widget.EditText.getText() 我在另一个对话框上放大视图,它可以正常工作,但是每当我在对话框中单击ok时,我总是得到上面的错误尝试在空对象引用上调用虚拟方法android.widget.EditText.getText(),android,android-edittext,android-alertdialog,Android,Android Edittext,Android Alertdialog,试图从对话框内的EditText获取Edit Text的值,但一次又一次地出现此错误 尝试调用虚拟方法“android.text.Editable” 空对象引用上的android.widget.EditText.getText() 我在另一个对话框上放大视图,它可以正常工作,但是每当我在对话框中单击ok时,我总是得到上面的错误 LayoutInflater li = LayoutInflater.from(getContext()); View promptsVi
LayoutInflater li = LayoutInflater.from(getContext());
View promptsView = li.inflate(R.layout.custom_dimension_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getContext());
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
// edit text
widthEditText = findViewById(R.id.Width);
heightEditText = findViewById(R.id.Height);
String width = widthEditText.getText().toString();
String height = heightEditText.getText().toString();
Toast.makeText(mContext, "Values are: " + width + " " + height, Toast.LENGTH_SHORT).show();
// WIDTH=width
// HEIGHT=height;
}
})
.setNegativeButton("Cancel",
new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
您需要从对话框视图中查找EditText,如
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
您没有向edittext提供任何视图,请以这种方式更改代码
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
您需要以这种方式使用视图
promptView.findviewbyId
@Umar检查我的答案它会work@MilanPansuriya再次感谢你,伙计@奥马尔接受我的回答
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);