我为对话框创建了一个布局,并在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);
.../...
.../...
.../...
在onPrepareDialog下,我正在尝试获取我在EditText中输入的文本,这会引发空指针异常。请帮助解决问题
@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
答案 0 :(得分:4)
更改
mListNameEditText = (EditText) findViewById(R.id.listNameText);
到
mListNameEditText = (EditText) dialog.findViewById(R.id.listNameText);
您所拥有的一行是尝试在主布局中找到EditText(使用setContentView(R.layout.whatever);
设置的那个)。通过将其更改为我发布的行,您将在该对话框中搜索EditText
此外,在你的onCreateDialog中你也在做同样的事情。你还需要在那里使用dialog.findViewById(...)