在我看来,当窗口打开时,我有一个选择监听器。 (视图采用所选项目并根据该项目显示一些数据)。
关闭并重新打开视图时会发生异常。在另一个窗口中更改选择时更新视图的功能有效但在重新打开视图后视图未更新。
我有一个带有setText方法的标签,它给我这个小部件处理异常。
这是一个选择侦听器问题吗?在处理视图时,有必要删除在执行期间创建的所有侦听器吗?
修改
选择侦听器代码:
final ISelectionListener selectionListener=(new ISelectionListener()
{
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
// TODO Auto-generated method stub
IProject project = null ;
if(selection==null){
try {
throw new ProjectNotSelected("No selection in Window");
} catch (ProjectNotSelected e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection)selection).getFirstElement();
if (element instanceof IResource) {
project= ((IResource)element).getProject();
}
else if (element instanceof IPackageFragmentRoot) {
IJavaProject jProject = ((IPackageFragmentRoot)element).getJavaProject();
project = jProject.getProject();
}
else if (element instanceof IJavaElement) {
IJavaProject jProject= ((IJavaElement)element).getJavaProject();
project = jProject.getProject();
}
else{
try {
throw new ProjectNotSelected("case 1");
} catch (ProjectNotSelected e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
else{
try {
throw new ProjectNotSelected("case 2");
} catch (ProjectNotSelected e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(project!=null)
{
String projectName=project.getName();
projectNameLabel.setText("Project: "+projectName);
}
}
});
添加选择侦听器的函数:
public void newGetCurrentProject(final ISelectionListener selectionListener)
{
IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow window =workbench.getActiveWorkbenchWindow() ;
final IPartService pS=window.getPartService();
pS.addPartListener(new IPartListener()
{
@Override
public void partActivated(IWorkbenchPart part) {
// TODO Auto-generated method stub window.getSelectionService().addSelectionListener("org.eclipse.jdt.ui.PackageExplorer",selectionListener);
}
@Override
public void partBroughtToTop(IWorkbenchPart part) {
// TODO Auto-generated method stub
}
@Override
public void partClosed(IWorkbenchPart part) {
// TODO Auto-generated method stub
window.getSelectionService().removeSelectionListener("org.eclipse.jdt.ui.PackageExplorer",selectionListener);
}
@Override
public void partDeactivated(IWorkbenchPart part) {
// TODO Auto-generated method stub
}
@Override
public void partOpened(IWorkbenchPart part) {
// TODO Auto-generated method stub
window.getSelectionService().addSelectionListener("org.eclipse.jdt.ui.PackageExplorer",selectionListener);
}
});
堆栈跟踪:
org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:4361)
at org.eclipse.swt.SWT.error(SWT.java:4276)
at org.eclipse.swt.SWT.error(SWT.java:4247)
at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:340)
at org.eclipse.swt.widgets.Label.setText(Label.java:386)
at com.warnings.compilation.views.TabViewer$2.selectionChanged(TabViewer.java:260)
at org.eclipse.ui.internal.AbstractPartSelectionTracker$1.run(AbstractPartSelectionTracker.java:119)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.runtime.Platform.run(Platform.java:857)
at org.eclipse.ui.internal.AbstractPartSelectionTracker.fireSelection(AbstractPartSelectionTracker.java:117)
at org.eclipse.ui.internal.WindowPartSelectionTracker$1.selectionChanged(WindowPartSelectionTracker.java:38)
at org.eclipse.ui.internal.AbstractPartSelectionTracker$1.run(AbstractPartSelectionTracker.java:119)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.runtime.Platform.run(Platform.java:857)
at org.eclipse.ui.internal.AbstractPartSelectionTracker.fireSelection(AbstractPartSelectionTracker.java:117)
at org.eclipse.ui.internal.PagePartSelectionTracker$1.selectionChanged(PagePartSelectionTracker.java:49)
at org.eclipse.jface.viewers.Viewer$2.run(Viewer.java:164)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:49)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at org.eclipse.jface.viewers.Viewer.fireSelectionChanged(Viewer.java:162)
at org.eclipse.jface.viewers.StructuredViewer.updateSelection(StructuredViewer.java:2188)
at org.eclipse.jface.viewers.StructuredViewer.handleSelect(StructuredViewer.java:1211)
at org.eclipse.jface.viewers.StructuredViewer$4.widgetSelected(StructuredViewer.java:1241)
at org.eclipse.jface.util.OpenStrategy.fireSelectionEvent(OpenStrategy.java:239)
at org.eclipse.jface.util.OpenStrategy.access$4(OpenStrategy.java:233)
at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:403)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
选择侦听器放在createPartControl中。
问题出现在这一行:
projectNameLabel.setText("Project: "+projectName);
答案 0 :(得分:3)
在视图部件中添加选择侦听器时,必须在视图关闭时删除侦听器。类似的东西:
public classs MyView extends ViewPart
{
private ISelectionListener myListener;
@Override
public void createPartControl(final Composite parent)
{
myListener = .... create listener
ISelectionService selService = ... get selection serivce
selService.addSelectionListener(myListener);
}
@Override
public void dispose()
{
if (myListener != null)
{
ISelectionService selService = ... get selection serivce
selService.removeSelectionListener(myListener);
myListener = null;
}
super.dispose();
}
}