android smack MultiUserChat.getHostedRooms( NullPointerException)
2013-07-18 21:06
288 查看
用Smack开发andorid IM, 开发群组时想获取服务器上所有的聊天室,调用下面方法时出现NullPointerException:
而方法的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,代码为:
Class.forName("org.jivesoftware.smackx.ServiceDiscoveryManager", true, ClientConnectionServer.class.getClassLoader());
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
// 下面是进行XMPPConnection的初始化 [/code]
........
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]
........
相关文章推荐
- java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportant
- java.lang.NullPointerException at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked()
- java.lang.NullPointerException at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked()
- android nullpointerexception ContextWrapper.getResources()
- ava.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddi
- java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportant
- java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImporta..
- getHibernateTemplate()中的方法报java.lang.NullPointerException的解决办法
- android NullPointerException RecyclerView.onMeasure
- Android 解决方案:Caused by: java.lang.NullPointerException
- Android Switch 控件 空指针异常 NullPointerException
- Android: NullPointerException when using RelativeLayout with measure()
- context.getSystemService(context.LAYOUT_INFLATER_SERVICE)调用时报NullPointerException
- Cannot get Jedis connection; nested exception is java.lang.NullPointerException
- Android平台下基于XMPP的IM研究(二 MultiUserChat 聊天室)
- Android ListView onScroll NullPointerException ListView滚动崩溃
- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ScrollView.smo
- Android 由于SDK版本不同setUserVisibleHint造成的nullPointerexception
- Android中利用NetworkInfo判断网络状态时出现空指针(NullPointerException)问题解决
- 解决android集成环境ADT的java.lang.NullPointerException错误