0

我希望按照此处列出的示例将 ContextMenu 添加到 TreeViewer:http ://www.vogella.com/articles/EclipseCommands/article.html#contextmenu但是我在线获得了 NPE

getSite().registerContextMenu(menuManager, viewer);

getSite() 返回 null 而不是 IWorkbenchPartSite。谁能解释为什么 IWorkbenchPartSite 为空或如何初始化它?

任何帮助表示赞赏。

4

1 回答 1

0

There is no method getSite() in preference pages. So I assume you are in an editor or a view.

The site is injected by the platform using the init method. When you call getSite() you have to make sure that you do it after the platform has called the init method.

A good place for context menu registration is the createPartControl method.

于 2012-10-15T13:11:10.640 回答