将数据添加到TableViewer时出现异常

发布于 2024-12-23 11:22:12 字数 4745 浏览 2 评论 0 原文

我在组合中有 2 个 TableViewer 控件(假设它们是:viewer1viewer2)。初始化应用程序时,一个“viewer1”从服务器填充数据,而“viewer2”设置为空。

viewer1.setInput(getData());
viewer2.setInput(null);

现在我想通过上下文菜单将某些对象从viewer1更改为viewer2,第一次添加成功,但是下次当我将对象填充到viewer2时,我总是得到这个异常: java.lang.UnsupportedOperationException

如果 viewer2 预先包含一些对象,则不会发生这种情况,仅当 viewer2 为空时才会发生。 这是我添加将一些数据从 viewer1 移动到 viewer2 的方法:

//get selected objects in Viewer1
IStructuredSelection selection= (IStructuredSelection) viewer1.getSelection();
if(selection!=null){
            selectedList=(List<MyObject>)selection.toList();
            for(MyObject obj: selectedList){
                //do something here
            }
            //remove all
            ((List<MyObject>)viewer1().getInput()).removeAll(selectedList);
            viewer1.refresh();
        }
//viewer1 get above List
if((List<MyObject>)viewer2.getInput()==null)
 viewer2.setInput(selectedList);
else {
 ((List<MyObject>)viewer2.getInput()).addAll(selectedList);  //(Line Error)
}
viewer2.refresh;

第一次在 IF 条件下效果很好,但下次在 viewer2< /strong> 包含一些数据,我总是在上面的错误行得到异常。 @Krumelur,我添加了我得到的堆栈跟踪:

!ENTRY org.eclipse.ui 4 0 2011-12-27 16:26:50.660
!MESSAGE Unhandled event loop exception
!STACK 0
java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractCollection.addAll(Unknown Source)
    at com.saltlux.om.client.customwidgets.NeutralSetWidget.getReviewFromOtherWidget(NeutralSetWidget.java:39)
    at com.saltlux.om.client.customwidgets.NeutralSetWidget$1.handleEvent(NeutralSetWidget.java:24)
    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.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
    at com.saltlux.om.client.customwidgets.PositiveSetWidget.add2TrainingSet(PositiveSetWidget.java:107)
    at com.saltlux.om.client.customwidgets.TrainingReviewWidget$10.run(TrainingReviewWidget.java:288)
    at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
    at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
    at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
    at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
    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:4165)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at com.saltlux.om.client.Application.start(Application.java:21)
    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:344)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    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:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

请帮助我。非常感谢您的任何答复!

I have 2 TableViewer controls in a composite (Supposed they are : viewer1, viewer2). When initialize application, one "viewer1" is filled data from server while "viewer2" is set null.

viewer1.setInput(getData());
viewer2.setInput(null);

Now I want to change some object from viewer1 to viewer2 via a context menu, the first time add is successful, but the next time when I fill object to viewer2, I always get this exception:
java.lang.UnsupportedOperationException

This didn't happen if viewer2 contain some objects in advance, it only happens when viewer2 is empty.
This is the way I add move some data from viewer1 to viewer2 :

//get selected objects in Viewer1
IStructuredSelection selection= (IStructuredSelection) viewer1.getSelection();
if(selection!=null){
            selectedList=(List<MyObject>)selection.toList();
            for(MyObject obj: selectedList){
                //do something here
            }
            //remove all
            ((List<MyObject>)viewer1().getInput()).removeAll(selectedList);
            viewer1.refresh();
        }
//viewer1 get above List
if((List<MyObject>)viewer2.getInput()==null)
 viewer2.setInput(selectedList);
else {
 ((List<MyObject>)viewer2.getInput()).addAll(selectedList);  //(Line Error)
}
viewer2.refresh;

It works well at the first time in the IF condition, but the next time when viewer2 contained some data, I always get exception at above error line.
@Krumelur , I added stacktrace that I got:

!ENTRY org.eclipse.ui 4 0 2011-12-27 16:26:50.660
!MESSAGE Unhandled event loop exception
!STACK 0
java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractCollection.addAll(Unknown Source)
    at com.saltlux.om.client.customwidgets.NeutralSetWidget.getReviewFromOtherWidget(NeutralSetWidget.java:39)
    at com.saltlux.om.client.customwidgets.NeutralSetWidget$1.handleEvent(NeutralSetWidget.java:24)
    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.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
    at com.saltlux.om.client.customwidgets.PositiveSetWidget.add2TrainingSet(PositiveSetWidget.java:107)
    at com.saltlux.om.client.customwidgets.TrainingReviewWidget$10.run(TrainingReviewWidget.java:288)
    at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
    at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
    at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
    at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
    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:4165)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at com.saltlux.om.client.Application.start(Application.java:21)
    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:344)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    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:622)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

Please help me out. Thanks so much for any answer!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦醒时光 2024-12-30 11:22:12

并非所有 java.util.List 接口的实现都支持 addAll() 方法。例如, Arrays.asList() 不会,这是 StructuredSelection.toList()

Not all implementations of java.util.List interface support addAll() method. For example, the one that is returned by Arrays.asList() does not and this is the method used by StructuredSelection.toList().

浅浅淡淡 2024-12-30 11:22:12

听起来您的内容提供商抛出了异常。您是否检查过您的内容提供商确实接受该列表?您使用什么内容提供商?

It sounds like the exception is thrown from your content provider. Have you checked that your content provider actually accepts the List? What content provider do you use?

萌辣 2024-12-30 11:22:12

我找到了一个解决方法,更改了 viewer2 的初始化输入的方式,如下所示:

viewer2.setInput(new ArrayList<MyObject>())

在我为输入设置 NUll 之前,无法使用方法 addAll() 向 tableviewer 添加更多对象。
感谢大家的帮助。
此致!

I found out a workaround, change the way to initialize input for viewer2 as follow:

viewer2.setInput(new ArrayList<MyObject>())

Before I set NUll for input, and couldn't use method addAll() to add more objectes to tableviewer.
Thanks all for your help.
Best regards!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文