Reputation: 105
Trying to get the value of Edit Text from a EditText inside the Dialog but getting this error again and again
Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
I am inflating a view over another dialog it works fine but whenever I click ok in the dialog I keep getting the above error
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();
Upvotes: 2
Views: 139
Reputation: 579
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
you need to use your view in this way promptView.findviewbyId
Upvotes: 1
Reputation: 2559
You are not providing any view to the edittext change your code this way
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
Upvotes: 1
Reputation: 58934
You need finding EditText from your dialog view like.
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);
Upvotes: 1