无法创建部件的控件树查看器。 ClassCastException异常

时间:2015-04-15 09:46:21

标签: java eclipse treeview rcp classcastexception

我想在我的RCP应用程序中创建一个项目树资源管理器。
这是我对树的看法:

public class ProjectExplorer extends ViewPart{  
    public static final String ID = "rcp.projectexplorer";
    public ProjectExplorer() {
    }
    private TreeViewer viewer;
    public void createPartControl(Composite parent) {
        viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        viewer.setInput(ProjectManager.getInstance().getProjects().toArray());
    }

    @Override
    public void setFocus() {
        viewer.getControl().setFocus();
    }

}

ProjectManager.getInstance().getProjects().toArray() - 返回包含当前项目的数组。 每个项目都实现了接口IProjectNode
在ContentProvider中,方法Object[] getElements(Object inputElement)我尝试将项目(inputElement)从Object[]投射到IProjectNode[]

public class ViewContentProvider implements ITreeContentProvider {

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

    }

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        // TODO Auto-generated method stub

    }

    @Override
    public Object[] getElements(Object inputElement) {
        return (IProjectNode[])inputElement;
    }

    @Override
    public Object[] getChildren(Object parentElement) {
        return ((IProjectNode)parentElement).getChildren();
    }

    @Override
    public Object getParent(Object element) {
        // TODO Auto-generated method stub
        return ((IProjectNode)element).getParent();
    }

    @Override
    public boolean hasChildren(Object element) {
        if (((IProjectNode)element).getChildren() == null)
            return true;
        return false;
    }

}

但我有以下错误:"无法创建部件的控件"。

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lrcp.projects.source.IProjectNode;
at rcp.projects.ViewContentProvider.getElements(ViewContentProvider.java:24)
at org.eclipse.jface.viewers.StructuredViewer.getRawChildren(StructuredViewer.java:999)
at org.eclipse.jface.viewers.ColumnViewer.getRawChildren(ColumnViewer.java:699)

项目中的每个孩子也实施IProjectNode

我如何修复此异常?我做错了什么?
关于儿童方法的信息:

public boolean hasChildren(Object element) {'
        if (((IProjectNode)element).getChildren() == null)
            return false;
        return true;
    }


实现' getChildren()'在项目类中:

public IProjectNode[] getChildren() {
        IProjectNode[] projectNodes = {
                settingsFile,
                ldlFile,
                pythonFolder,
                outFolder
                };
        return projectNodes;
    }


settingsFile, ldlFile, pythonFolder, outFolder也实施了IProjectNode

1 个答案:

答案 0 :(得分:0)

ProjectManager.getInstance().getProjects().toArray();

toArray()方法返回一个Object数组,您无法将其转换为IProjectNode数组(即使实际内容是这些节点的数组)。

改为使用:

ProjectManager.getInstance().getProjects().toArray(new IProjectNode[0]);

这种形式的toArray会返回IProjectNode

的数组

此外,您的hasChildren方法正在返回' true'当项目节点' getChildren'返回null和' false'当有孩子的时候。你需要这样的东西:

@Override
public boolean hasChildren(Object element) {
    return ((IProjectNode)element).getChildren() != null;
}