用Smack開發andorid IM, 開發羣組時想獲取服務器上全部的聊天室,調用下面方法時出現NullPointerException:html
Collection<HostedRoom> hostrooms = MultiUserChat.getHostedRooms(ClientConnectionServer.connection, ClientConnectionServer.connection.getServiceName());
而方法的2個參數都不爲空,感受很怪,就去國外論壇看了緣由,查了幾篇文章發現解決辦法,下面是論壇裏的一些信息:java
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.web
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.api
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 的:oracle
因此在初始化 XMPPConnection 前先初始化ServiceDiscoveryManager,代碼爲:app
try { // 其中ClientConnectionServer.class.getClassLoader() 用來獲取當前類的類加載器,若是用........
Class.forName("org.jivesoftware.smackx.ServiceDiscoveryManager", true, ClientConnectionServer.class.getClassLoader()); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } // 下面是進行XMPPConnection的初始化 ClassLoader.getSystemClassLoader(),我測試的沒起做用