0

我正在尝试弹出一个对话框,显示有关 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();
  }
4

1 回答 1

6

采用

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

代替

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

从对话框布局访问 EditText

于 2013-01-15T11:40:38.863 回答