Reputation: 691
So I have made a custom Dialog Box in my app that contains a EditText field specifically formatted for numbers. I have the code set up to launch the Dialog when the user clicks a button. The issue I'm having is that when the user confirms the information they've entered, I'm getting a error like this:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
Any idea what I might be doing wrong? Here is the complete code for my Dialog Box inflater and the button click handlers.
public Dialog onCreateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
Log.i("INFO","Creating Dialog...");
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.goaldialog, null))
// Add action buttons
.setPositiveButton(R.string.SetAGoalButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText) findViewById(R.id.GoalChooser);
Integer goalNumber = Integer.parseInt(et.getText().toString());
Log.i("INFO",Integer.toString(goalNumber));
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.show();
return builder.create();
}
I'm new to android and Java, so help would be appreciated.
UPDATE intrepidkarthi's answer solved my initial problem, but now the string that is getting returned looks like this:
07-22 14:13:43.687 29972-29972/com.collusion.serviceassistant D/INFO﹕ [ 07-22 14:13:43.687 29972:29972 D/AndroidRuntime ]
Shutting down VM
I'm getting the text from EditText using this:
String goalNumberString = String.valueOf(et.getText());
Upvotes: 2
Views: 3614
Reputation: 9
when we use inflater.inflate from a layout xml file,we need to add the view before findViewById。
View view = inflater.inflate(R.layout.goaldialog, null);
EditText et = (EditText)view.findViewById(R.id.GoalChooser);
Upvotes: 0
Reputation: 3102
Try this.
public Dialog onCreateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
Log.i("INFO","Creating Dialog...");
View view = inflater.inflate(R.layout.goaldialog, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(view)
// Add action buttons
.setPositiveButton(R.string.SetAGoalButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText)view.findViewById(R.id.GoalChooser);
Integer goalNumber = Integer.parseInt(et.getText().toString());
globalVariable = goalNumber;
Log.i("INFO",Integer.toString(goalNumber));
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.show();
return builder.create();
}
Upvotes: 3
Reputation: 838
EditText et = (EditText)onCreateDialog().findViewById(R.id.GoalChooser);
Put that in here like this:
public Dialog onCreateDialog() {
final EditText et = (EditText) findViewById(R.id.GoalChooser);
does it work? im just guessing, the problem is your EditText is null since you are referencing in from another context (you are not using the context of the activity to instantiate that edit text).
Upvotes: 0
Reputation: 643
This is wrong instruction:
EditText et = (EditText)onCreateDialog().findViewById(R.id.GoalChooser);
Example:
mEdit = (EditText)findViewById(R.id.edittext);
String content = mEdit.getText().toString();
Upvotes: 0
Reputation: 1233
capture the View instance into a variable when you use setView(), then you can use findViewById to get the EditText:
final View view=inflater.inflate(R.layout.goaldialog, null);
builder.setView(view)
.setPositiveButton(R.string.SetAGoalButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText)view.findViewById(R.id.GoalChooser);
Integer goalNumber = Integer.parseInt(et.getText().toString());
Log.i("INFO",Integer.toString(goalNumber));
dialog.dismiss();
}
})
Upvotes: 0