1 , RCP: getActiveWorkbenchWindow() returns null
Winzard结束后,想要打开一个editor,不过 getActiveWorkbenchWindow() returns null
问题及解决如下:
原因:
getActiveWorkbenchWindow()
null can be returned for the active workbench window if:
there is a non-workbench window active (modal dialog)
you are not in a "UI thread". You will need to investigate
Display.asyncExec and Display.syncExec
--------------------------------------------------------------------------------
In my wizard page I ve the following code to open a diagram when the wizard process is ended :
private void openEditor(IFile file) {
try {
IWorkbenchWindow dwindow = workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = dwindow.getActivePage();
if (page != null) {
IDE.openEditor(page, file, true);
}
} catch (PartInitException pie) {
Logger.error("Unable to open the Editor", pie);//$NON-NLS-1$
throw new PageException("Unable to open the Editor", pie);
}
}
but when I execute it I got a null pointer exception while I'm trying to call workbench.getActiveWorkbenchWindow(); the workbench is not null and don't know why it's append
-------------------------------------------------------------------------------
Finaly solved by using
private void openEditor(IFile file) {
final IFile aFile = file;
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = dwindow.getActivePage();
if (page != null) {
try {
IDE.openEditor(page, aFile, true);
}catch (PartInitException pie) {
Logger.error("Unable to open the Editor", pie);//$NON-NLS-1$
throw new PageException("Unable to open the Editor", pie);
}
}
}
});
}
解决方法
*******************************************************非UI线程到UI线程内操作*****************************************************
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
..........some code
}
});
2,