带有EditText的Android Dialog会抛出NullPointerException

Bra*_*rae 0 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();
  }
Run Code Online (Sandbox Code Playgroud)

ρяσ*_*я K 6

使用

EditText descriptionOutput = (EditText)dialog.findViewById(R.id.dialogText);
Run Code Online (Sandbox Code Playgroud)

代替

EditText descriptionOutput = (EditText) findViewById(R.id.dialogText);
Run Code Online (Sandbox Code Playgroud)

从对话框布局访问EditText