我一直在开发OpenOffice Writer扩展。当前,我需要检索当前文档对象。以下内容应将当前组件作为Writer文档返回。

XMultiComponentFactory factory = context.getServiceManager();
XDesktop desktop = (XDesktop) factory.createInstanceWithContext("com.sun.star.frame.Desktop", context);
XTextDocument document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, desktop.getCurrentComponent());

return document;


我的问题在第二行运行时发生。引发以下异常:

java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to com.sun.star.frame.XDesktop


为什么createInstanceWithContext返回不符合所请求接口的内容?有办法解决吗?

最佳答案

查看文档here,有两个步骤可以获取界面:

          Object desktop = xRemoteServiceManager.createInstanceWithContext (
              "com.sun.star.frame.Desktop", xRemoteContext);
          XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, desktop);

关于java - 尝试使用OpenOffice API获取XDesktop实例时发生ClassCastException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18084326/

10-10 16:00