Eclipse rcp comboViewer.getSelection()在eclipse rcp中返回null

Eclipse rcp comboViewer.getSelection()在eclipse rcp中返回null,eclipse-rcp,Eclipse Rcp,我在TitleAreaDialog框中创建了一个名为ComboViewer的ComboViewer。 查看器由TreeViewer中定义的各种类别填充 但会在以下行中抛出NullPointerException: ISelection categorySelection = comboViewer.getSelection(); 为什么会这样?解决方法是什么?您可以使用此“模式”解决此问题,即在设置对话框时: final ISelection selection = new ISelect

我在
TitleAreaDialog
框中创建了一个名为
ComboViewer
ComboViewer
。 查看器由
TreeViewer
中定义的各种类别填充

但会在以下行中抛出NullPointerException:

ISelection categorySelection = comboViewer.getSelection();

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

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

  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();
    }
  };
(对于您的案例,请使用TitlearReadialog而不是Dialog。)


也就是说,覆盖对话框的方法。如果用户进行了选择,然后按“确定”,则选择[0]将包含该选择。

该行代码在哪里?我们需要看更多的代码。例如,如果您在对话框关闭后调用
comboViewer.getSelection()
,这对您不起作用。嘿,谢谢您的帮助,但问题仍然存在。还有其他出路吗?在okPressed()中调用comboViewer.getSelection()。