您的位置:首页 > 移动开发 > Android开发

android smack MultiUserChat.getHostedRooms( NullPointerException)

2013-07-18 21:06 288 查看
用Smack开发andorid IM, 开发群组时想获取服务器上所有的聊天室,调用下面方法时出现NullPointerException:

Collection<HostedRoom> hostrooms = MultiUserChat.getHostedRooms(ClientConnectionServer.connection, ClientConnectionServer.connection.getServiceName());


而方法的2个参数都不为空,感觉很怪,就去国外论坛看了原因,查了几篇文章发现解决办法,下面是论坛里的一些信息:

The problem is that the static block of the ServiceDiscoveryManager class has to be evaluated beforeany connection is created. In smack this is done via an configuration file, but this approach does not work on Android and therefore on aSmack.

The workaround mentioned in the answer is somehow ugly, since you really don't want to use the Constructor to fetch the SDM object, instead the get() method should be used. But the get() method only works if there was actually a SDM created for the connection.

So in order to init the SDM correctly on Android you need to call the full forName notation to init the static blocks of the class before you create the first (XMPP)Connection object.。

上面方法是针对 aSmack 问题给出的解决方法,同样也适用 纯smack 的:

所以在初始化 XMPPConnection 前先初始化ServiceDiscoveryManager,代码为:

try
{
  // 其中ClientConnectionServer.class.getClassLoader() 用来获取当前类的类加载器,如果用[code]ClassLoader.getSystemClassLoader(),我测试的没起作用

  Class.forName("org.jivesoftware.smackx.ServiceDiscoveryManager", true, ClientConnectionServer.class.getClassLoader());
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}

// 下面是进行XMPPConnection的初始化 [/code]
  ........
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐