Attempt to invoke virtual method on Attempt to invoke virtual method android.widget.EditText.getText() 对空对象引用
Attempt to invoke virtual method on Attempt to invoke virtual method android.widget.EditText.getText() on a null object reference
试图从对话框内的 EditText 中获取 Edit Text 的值,但一次又一次地出现此错误
Attempt to invoke virtual method 'android.text.Editable
android.widget.EditText.getText()' on a null object reference
我正在通过另一个对话框扩充视图它工作正常但是每当我在对话框中单击“确定”时我总是收到上述错误
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);
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
您需要以这种方式使用您的视图promptView.findviewbyId
试图从对话框内的 EditText 中获取 Edit Text 的值,但一次又一次地出现此错误
Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
我正在通过另一个对话框扩充视图它工作正常但是每当我在对话框中单击“确定”时我总是收到上述错误
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);
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
您需要以这种方式使用您的视图promptView.findviewbyId