Jface:Listviewer抛出AssertionFailedException

时间:2013-12-04 20:42:50

标签: java listview swt jface

我想创建一个Jface Listviewer,但它总是抛出以下异常:

 org.eclipse.core.runtime.AssertionFailedException: assertion failed: 
        at org.eclipse.core.runtime.Assert.isTrue(Assert.java:110)
        at org.eclipse.core.runtime.Assert.isTrue(Assert.java:96)
        at org.eclipse.jface.viewers.StructuredViewer.assertContentProviderType(StructuredViewer.java:1671)
        at org.eclipse.jface.viewers.StructuredViewer.setContentProvider(StructuredViewer.java:1661)
        at property.sheet.internalConstraint.ConstraintDialog.createDialogArea(ConstraintDialog.java:26)
        at org.eclipse.jface.dialogs.Dialog.createContents(Dialog.java:775)
        at org.eclipse.jface.window.Window.create(Window.java:432)
        at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1104)
        at org.eclipse.jface.window.Window.open(Window.java:791)
 ....

我的createDialogArea方法(我在其中创建ListViewer)如下所示:

protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        lv = new ListViewer(container, SWT.SINGLE);
        lv.setContentProvider(new ConstraintDialogContentProvider());
        lv.setLabelProvider(new ConstraintDialogLabelProvider());
        lv.setInput(fm);
        return container;
    }

通过调试,我发现在设置contentProvider之后抛出了异常:

public class ConstraintDialogContentProvider implements IContentProvider {

    private List<Feature> features = new ArrayList<Feature>();

    @Override
    public void dispose() {
    }

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    }

    public Object[] getElements(Object input) {
        if (input instanceof FeatureMap) {
            FeatureMap fm = (FeatureMap) input;
            if (fm.getFeature() != null) {
                getAllFeatures(fm.getFeature());
                return features.toArray();
            } else {
                return null;
            }
        }
        return null;
    }

    private void getAllFeatures(Feature feature) {
        features.add(feature);
        .....
                // adds additional features
    }
}

我真的不明白我在这里做错了什么。有什么想法吗?

干杯, 菲尔

1 个答案:

答案 0 :(得分:1)

ListViewer的内容提供商必须实施IStructuredContentProvider,而不仅仅是IContentProviderassertContentProviderType来电正在检查此内容。