0

我在一个盒子里创建了一个ComboViewer名字。查看器由 中定义的各种类别填充。comboViewerTitleAreaDialogTreeViewer

但在以下行会引发 NullPointerException:

ISelection categorySelection = comboViewer.getSelection();

为什么会这样?解决方法是什么?

4

1 回答 1

0

您可以使用此“模式”解决此问题,即在设置对话框时:

  final ISelection selection = new ISelection[1];

  Dialog diag = new Dialog(shell) {
    // createDialogArea()
    // remember your comboViewer as field


    @Override
    protected void okPressed() {
      selection[0] = comboViewer.getSelection();
      super.okPressed();
    }
  };

(在您的案例中使用 TitleAreaDialog 而不是 Dialog。)

即覆盖对话框的okPressed 方法。如果用户进行了选择,然后按 OK,则 selection[0] 将包含该选择。

于 2012-10-11T12:49:31.340 回答