Can't create a multi user chat (MUC) room with aSmack library for Android: "packet.DefaultPacketExtension cannot be cast to packet.MUCUser"

Stephan Celis picture Stephan Celis · Mar 2, 2013 · Viewed 9.1k times · Source

For an application I need to be able to create a multi user chatroom and join it. The chat server is a openfire server.

I used to have:

MultiUserChat chat = new MultiUserChat(connection, roomName + "@conference.localhost");
chat.join(nickname);

When the room doesn't exist it creates the room and joins. however the next user can't join. He get's a 404, recipient unavailable(404). Which suggests the chatroom is locked or something.

Then I found code in the documentation and I tried the following:

chat.create(nickname);

// Send an empty room configuration form which indicates that we want
// an instant room
chat.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));

However when I try to execute this it says it crashes with the following error in the logcat:

03-02 12:04:12.890: E/AndroidRuntime(20872): FATAL EXCEPTION:

 AsyncTask #3 03-02 12:04:12.890: E/AndroidRuntime(20872):

 java.lang.RuntimeException: An error occured while executing
 doInBackground() 
03-02 12:04:12.890: E/AndroidRuntime(20872): Caused
 by: java.lang.ClassCastException:

 org.jivesoftware.smack.packet.DefaultPacketExtension cannot be cast to
 org.jivesoftware.smackx.packet.MUCUser 03-02 12:04:12.890:
 E/AndroidRuntime(20872):   at
 org.jivesoftware.smackx.muc.MultiUserChat.getMUCUserExtension(MultiUserChat.java:2002)
 03-02 12:04:12.890: E/AndroidRuntime(20872):   at
 org.jivesoftware.smackx.muc.MultiUserChat.create(MultiUserChat.java:364)
 03-02 12:04:12.890: E/AndroidRuntime(20872):   at
 be.xios.crs.pivi.managers.XmppManager.createOrJoinChatGroup(XmppManager.java:116)

Hope someone can help me with this and give some advise.

Answer

Flow picture Flow · Mar 2, 2013

Edit 04/2015: Calling SmackAndroid.init(Context) is no longer required with Smack 4.1 (or higher). In fact SmackAndroid does no longer exist.

org.jivesoftware.smack.packet.DefaultPacketExtension cannot be cast to org.jivesoftware.smackx.packet.MUCUser is usually an indicator that you didn't initialized the static code of aSmack that takes care of registering the Smack providers for the various packet (and packet extension) classes of Smack.

For most aSmack versions, simply calling

SmackAndroid.init(Context ctx)

will do the trick.