i created a layout for my dialog and inflate the layout in OnCreateDialog
/**
* Object to hold the reference to ListName EditText from Alert_Dialog
*/
private EditText mListNameEditText;
protected Dialog onCreateDialog(int id) {
switch(id) {
case DIALOG_TEXT_ENTRY:
Log.w(TAG, "In onCreateDialog");
LayoutInflater factory = LayoutInflater.from(this);
final View alertDialogView = factory.inflate(R.layout.alert_dialog, null);
mListNameEditText = (EditText) findViewById(R.id.listNameText);
.../...
.../...
.../...
Under onPrepareDialog, i’m trying to get the text that i typed inside the EditText, and this throws a null pointer exception. Please help figuring out the issue
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch(id) {
case DIALOG_TEXT_ENTRY:
Log.w(TAG, "In onPrepareDialog");
mListNameEditText = (EditText) findViewById(R.id.listNameText);
if(mListNameEditText == null)
Log.w(TAG, "ListName EditText is null"); // This text is printed !!
mListNameEditText.setOnKeyListener(new OnKeyListener() { // Exception here because mListNameEditText is NULL
Change
to
The line you have is trying to find the EditText in the main layout (the one you set using
setContentView(R.layout.whatever);). By changing it to the line I posted, you are searching within that dialog for the EditTextAdditionally, in your onCreateDialog you are doing the same thing. You need to use dialog.findViewById(…) there as well