gpt4 book ai didi

java - Osgi 中的bundleContext.getServiceReference

转载 作者:行者123 更新时间:2023-12-01 13:55:52 26 4
gpt4 key购买 nike

我在运行 Java 应用程序时遇到问题,在 Applet Activator 类打开时在 OSGi 中进行一些搜索后,gui 的bundleContext.getServiceReference抛出一个NPE,我发现这种情况发生是因为没有注册实现UIService的服务。我的问题是:这是什么意思?我应该检查什么来修复它?

错误代码:

java.lang.NullPointerException
at net.java.sip.communicator.impl.appletActivator.AppletActivator.openMainGui(AppletActivator.java:57)
at net.java.sip.communicator.impl.appletActivator.AppletActivator.start(AppletActivator.java:40)
at org.apache.felix.framework.util.SecureAction$Actions.run(SecureAction.java:1243)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:620)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:1904)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1822)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1192)
at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:266)
at java.lang.Thread.run(Unknown Source)

来源:

public static <T> T getService(BundleContext bundleContext,
Class<T> serviceClass) {
//This throws the NullPointerException
ServiceReference serviceReference = bundleContext
.getServiceReference(serviceClass.getName());

return (serviceReference == null) ? null : (T) bundleContext
.getService(serviceReference);
}

我是 OSGi 新手,不理解其中的一些概念。

最佳答案

框架中是否安装了一些可以注册 UIService 的包?另外,您的代码似乎可能对启动顺序敏感,这在 OSGi 中是禁忌。调用 getService 方法并期望它返回服务假定注册该服务的包已经 (a) 已经启动并且 (b) 已经注册了该服务。这就是为什么您通常在 OSGi 应用程序中使用 DS 或 ServiceTrackers 来解耦 bundle 的启动顺序。

关于java - Osgi 中的bundleContext.getServiceReference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19632525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com