【发布时间】:2016-03-27 15:59:06
【问题描述】:
我正在开发一个需要访问视图的 Eclipse 插件。
我找到了这种获取工作台的方法,但我猜它只在 Eclipse 3.x 中有效,而我使用的是 Eclipse 4,所以它不起作用:
IWorkbench workbench = PlatformUI.getWorkbench();
IViewPart part = workbench.getActiveWorkbenchWindow().getActivePage()
.findView("id_of_the_view");
然后我发现是这样的:
@Inject
private static EPartService epartService;
MPart mPart = epartService.findPart("id_of_the_view");
MyViewClass part = (MyViewClass)mPart.getObject();
抛出 NullPointerException。我已确保视图已打开。
我错过了什么?
编辑:
我也试过这个,发现here (german)
@Inject
private static EPartService epartService;
@Inject
private static MApplication application;
@Inject
private static EModelService modelService;
...
MUIElement element = modelService.find("id_of_the_view", application);
if(element instanceof MPart) {
MPart part = (MPart) element;
...
}
但我在 .find() 行中也有一个 NPE。
【问题讨论】:
-
使用
EPartService是正确的。什么是 null - mPart 或 epartService?你确定 id 是正确的吗? -
不要将
static用于注入对象。你在哪里运行这段代码? -
仅注入由 Eclipse 从 Application.e4xmi 中描述的对象创建的类。如果您使用
new创建一个类,它不会被注入。 -
看起来您仍在使用 3.x 兼容模式,因此您不应该尝试使用注入。在这种情况下,PlatformUI 是正确使用的东西 - 但它必须作为 Eclipse 插件的一部分在正确初始化的 Eclipse 中运行。
-
您的代码必须在 Eclipse 插件中。