试图从对话框内的EditText获取Edit Text的值,但一次又一次地获得此错误
试图调用虚拟方法'android.text.Editable NULL对象引用上的android.widget.EditText.getText()'
我在另一个对话框上扩大视图的效果很好,但是每当我在对话框中单击“确定”时,我都会不断收到上述错误
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();
答案 0 :(得分:1)
您需要从对话框视图中找到EditText。
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
答案 1 :(得分:1)
您未提供任何视图以这种方式更改代码
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
答案 2 :(得分:1)
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
您需要以这种方式使用视图promptView.findviewbyId