ClassCastException when creating MUC room for XMPP

2019-02-18 17:36发布

问题:

I am using aSmack for creating chat application. When i am creating groupchat by using this aSmack it gives error. this is the code am using for creating GroupChat.

MultiUserChat muc = new MultiUserChat(connection, "xyz@abc.com");           

try {
    muc.create(u_name);              
    Form form = muc.getConfigurationForm();              
    Form submitForm = form.createAnswerForm();               
    for (Iterator<FormField> fields = form.getFields(); fields.hasNext();) {
        FormField field = (FormField) fields.next();
        if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {                 
            submitForm.setDefaultAnswer(field.getVariable());
        }
    }
    List<String> owners = new ArrayList<String>();
    Log.i(TAG, "list of owners=====" +owners.toString());
    owners.add(PmUser_name);
    submitForm.setAnswer("muc#roomconfig_roomowners", owners);               
    muc.sendConfigurationForm(submitForm);
} catch (XMPPException e) {          
    e.printStackTrace();
}

This is the jar i am using for aSmack asmack-2010.05.07.jar. it supports facebook xmpp, Gtalk xmpp and my own server xmpp chat. Now i want to create group chat for my own server, but it give this error.

E/AndroidRuntime(31002): Caused by: java.lang.ClassCastException:   
org.jivesoftware.smack.packet.DefaultPacketExtension
E/AndroidRuntime(31002):    at  
org.jivesoftware.smackx.muc.MultiUserChat.getMUCUserExtension(MultiUserChat.java:2000)
E/AndroidRuntime(31002):    at    
org.jivesoftware.smackx.muc.MultiUserChat.create(MultiUserChat.java:364)

I have search a lot but still didn't get any idea about how to solve this.

Any help would be appreciated before I pull my hair out.

回答1:

The Packet Providers of Smack where not registered. You get a ClassCastException because Smack is unable to create the correct Packet class instance for MUC. I suggest that you use a newer version of aSmack and follow the instructions in the README, which should initialize and register the Providers for you.