带有EditText的Android Dialog会抛出NullPointerException

时间:2013-01-15 11:38:32

标签: android listview dialog nullpointerexception android-edittext

我正在尝试弹出一个对话框,显示有关ListView中条目的更多信息。 ListView生成正常,对话框的所有变量都初始化正常,但是当我尝试将相关描述写入EditText框时,抛出NullPointerException。有什么想法吗?

@Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    //TODO Add code for action performed on item click here
    // i.e. open dialogue showing details

    // Custom dialog box
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.view_dialog);
    dialog.setTitle("Description: " + savedSubjects[position]);

    // set custom dialog components
    EditText descriptionOutput = (EditText) findViewById(R.id.dialogText);
    String descToWrite = savedDescriptions[position]; // I created this in case calling from the array was the problem. In the trace this variable is correctly set.

    descriptionOutput.setText(descToWrite); //the error occurs at this line


    // set dismiss button
    Button dialogButton = (Button) findViewById(R.id.dialogButton);
    //if button is clicked close the dialog
    dialogButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    // display the dialog
    dialog.show();
  }

1 个答案:

答案 0 :(得分:6)

使用

EditText descriptionOutput = (EditText)dialog.findViewById(R.id.dialogText);

而不是

EditText descriptionOutput = (EditText) findViewById(R.id.dialogText);

从对话框布局

访问EditText