NPE在HandledContributionItem.canExecuteItem中 [英] NPE in HandledContributionItem.canExecuteItem

查看:110
本文介绍了NPE在HandledContributionItem.canExecuteItem中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 plugin.xml 的工具栏的小编辑器:

I have this little editor with a toolbar contributed by the plugin.xml:

public class MyEditor extends EditorPart {

    public static final String ID = "org.acme.project.MyEditor";

    @Override
    public void init(IEditorSite site, IEditorInput input) throws PartInitException {
        setSite(site);
        setInput(input);
    }

    @Override
    public void createPartControl(Composite parent) {
        final ToolBarManager localToolBarmanager = new ToolBarManager();
        final IMenuService menuService = PlatformUI.getWorkbench().getService(IMenuService.class);
        menuService.populateContributionManager(localToolBarmanager, "toolbar:org.acme.menu");
        localToolBarmanager.createControl(parent);

    }
}

当然,我的 plugin.xml 有贡献:

<extension point="org.eclipse.ui.menus">
  <menuContribution allPopups="false" locationURI="toolbar:org.acme.menu">
        <command commandId="org.eclipse.ui.file.saveAll" style="push" />
    </menuContribution>
</extension>

编辑器打开,工具栏得到贡献,一切都很好。当我关闭打开编辑器时处于活动状态的工作台部件时,出现以下异常:

The editor opens, the toolbar gets contributed, everything is fine. When I close the workbench part that was active when I open the editor, I get the following exception:

!ENTRY org.eclipse.e4.ui.workbench.renderers.swt 4 2 2017-10-30 15:12:53.110
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.e4.ui.workbench.renderers.swt".
!STACK 0
java.lang.NullPointerException
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.canExecuteItem(HandledContributionItem.java:443)
    at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem$4.run(AbstractContributionItem.java:524)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.updateItemEnablement(AbstractContributionItem.java:564)
    at org.eclipse.e4.ui.workbench.renderers.swt.ToolItemUpdater.updateContributionItems(ToolItemUpdater.java:36)
    at org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer.subscribeTopicUpdateToolbarEnablement(ToolBarManagerRenderer.java:295)
    at sun.reflect.GeneratedMethodAccessor32.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
    at org.eclipse.e4.ui.internal.di.UIEventObjectSupplier$UIEventHandler$1.run(UIEventObjectSupplier.java:56)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:182)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4211)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3827)

这实际上只是关注的一部分。我可以关闭其他零件。此异常导致工具栏中的处理程序损坏,并且工具栏不再起作用。

And it's really only the one part that had focus. I can close other parts just fine. This exception leads to the handlers in the toolbar being broken and the toolbar not working anymore.

我发现了一些与该异常有关的错误:臭虫388516 & 错误394500

I found some bugs with that exception: Bug 388516 & Bug 394500

但是这些应该在4.2中已修复(我们正在使用4.6)。除此之外,我找不到任何可能导致此类奇怪错误消息的东西。

But these were supposedly fixed in 4.2 (and we are using 4.6). Other then that, I can't find anything which might result in weird error messages like that.

有人知道如何修复我们的工具栏吗?

Does anyone have an idea how to fix our toolbar?

推荐答案

这是愚蠢的E4错误之一。对我们来说可能排名前三。

That's one of the stupider E4 bugs. Probably Top 3 for us.

WorkbenchMenuService.populateContributionManager(ContributionManager,String)使用当前活动的工作台部分来注册菜单。由于工具栏通常是在 IWorkbenchPart.createPartControl(Composite)中创建的,这意味着尚未完全创建新零件,因此不是活动部分。

WorkbenchMenuService.populateContributionManager(ContributionManager, String) takes the currently active workbench part to register the menu for. Since toolbars are usually created in IWorkbenchPart.createPartControl(Composite), that means the new part was not yet fully created, and so is not yet the active part.

因此,工具栏不再对所有视图和编辑器都有效!

我找到了解决此大规模E4错误的解决方法(这确实很丑,但是您要怎么做?)。对 populateContributionManager 的调用必须替换为以下行:

I found a workaround (that is really ugly, but what are you going to do?) to fix this massive E4 bug. The call to populateContributionManager must be replaced with the following lines:

    final MPart part = getSite().getService(MPart.class);
    String menuLocation = "toolbar:org.acme.menu";

    if (menuService instanceof WorkbenchMenuService) {
        ((WorkbenchMenuService) menuService).populateContributionManager(part, manager, menuLocation);
    } else if (menuService instanceof SlaveMenuService) {
        ((SlaveMenuService) menuService).populateContributionManager(part, manager, menuLocation);
    } else
        throw new UnsupportedOperationException("Do not know how to handle " + menuService); //$NON-NLS-1$

这篇关于NPE在HandledContributionItem.canExecuteItem中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆