我正在尝试学习Treeviewer。但是,在运行代码时,执行该行时出现错误-

treeViewer.setContentProvider(new AddressContentProvider());


以下是我为View-创建的完整代码

public class ViewPart1 extends ViewPart {

public static final String ID = "HelloWorld.view1";
TreeViewer treeViewer;

public ViewPart1() {
    // TODO Auto-generated constructor stub
}

@Override
public void createPartControl(Composite parent) {
    Tree addressTree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL
            | SWT.V_SCROLL);
    addressTree.setHeaderVisible(true);

    TreeColumn column1 = new TreeColumn(addressTree, SWT.LEFT);
    addressTree.setLinesVisible(true);
    column1.setAlignment(SWT.LEFT);
    column1.setText("Land/Stadt");
    column1.setWidth(160);
    TreeColumn column2 = new TreeColumn(addressTree, SWT.RIGHT);
    column2.setAlignment(SWT.LEFT);
    column2.setText("Person");
    column2.setWidth(100);
    TreeColumn column3 = new TreeColumn(addressTree, SWT.RIGHT);
    column3.setAlignment(SWT.LEFT);
    column3.setText("m/w");
    column3.setWidth(35);

    treeViewer = new TreeViewer(addressTree);
    treeViewer.setContentProvider(new AddressContentProvider());
    treeViewer.setLabelProvider(new TableLabelProvider());
    List<City> cities = new ArrayList<City>();
    cities.add(new City());
    treeViewer.setInput(cities);
    treeViewer.expandAll();

}

@Override
public void setFocus() {
    // TODO Auto-generated method stub

}
}

My AddressContentProvider Class looks like this -

    public class AddressContentProvider implements IContentProvider {
    public Object[] getChildren(Object parentElement) {
        if (parentElement instanceof List)
            return ((List<?>) parentElement).toArray();
        if (parentElement instanceof City)
            return ((City) parentElement).getStreets();
        if (parentElement instanceof Street)
            return ((Street) parentElement).getHouses();
        return new Object[0];
    }

    public Object getParent(Object element) {
        if (element instanceof Street)
            return ((Street) element).city;
        if (element instanceof House)
            return ((House) element).street;
        return null;
    }

    public boolean hasChildren(Object element) {
        if (element instanceof List)
            return ((List<?>) element).size() > 0;
        if (element instanceof City)
            return ((City) element).getStreets().length > 0;
        if (element instanceof Street)
            return ((Street) element).getHouses().length > 0;
        return false;
    }

    public Object[] getElements(Object cities) {
        return getChildren(cities);
    }

    public void dispose() {
    }

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


当我尝试运行此命令时,出现此错误-

    !ENTRY org.eclipse.e4.ui.workbench 4 0 2015-08-27 10:10:25.121
!MESSAGE
!STACK 0
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.AbstractTreeViewer.assertContentProviderType(AbstractTreeViewer.java:2385)
    at org.eclipse.jface.viewers.TreeViewer.assertContentProviderType(TreeViewer.java:345)...


可能是什么原因?

编辑:当我在视图类中编写AddressContentProvider类时,它工作得很好。但是,我正在其他文件中编写此类,但它不起作用:(

最佳答案

Tree的内容提供程序必须实现ITreeContentProvider,而IContentProvider是不够的。

尽管您已经实现了树内容提供者的方法,但实际上必须声明该类实现了ITreeContentProvider

09-10 07:20