android无法在asmack中添加朋友

时间:2014-03-31 12:36:18

标签: android xmpp asmack

我是使用smack库并制作一个聊天应用程序的新手。当我添加好友时,从这段代码中......,它可以工作。

public void Addcontact() {    
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
Roster roster = m_connection.getRoster();

if(!roster.contains("abc@gmail.com")) {   
 try {           
        roster.createEntry("abc@gmail.com", "abc", null);               
    } catch (XMPPException e) {          
        e.printStackTrace();
    }
}else {
    Log.i("error= ", "contains");
}
}

此代码正在为添加单个朋友..,但现在我想动态添加名册中的朋友..,我试过这段代码

   private void addEntry(String jid, String user, String[] groups) {
    Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
     roster = commanInstant.connection.getRoster();

        try {           
            roster.createEntry(jid,user,null);               
            System.out.println(""+jid+user);
        } catch (XMPPException e) {          
            e.printStackTrace();
            System.out.println(""+e);
        }
    }
 }

所以它在按钮点击时给我空指针执行。

这是我的logcat

03-31 12:34:19.740: D/AndroidRuntime(4886): Shutting down VM
   03-31 12:34:19.770: E/AndroidRuntime(4886): FATAL EXCEPTION: main
  03-31 12:34:19.770: E/AndroidRuntime(4886): java.lang.NullPointerException
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at   org.jivesoftware.smack.packet.RosterPacket$Item.<init>(RosterPacket.java:117)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at org.jivesoftware.smack.Roster.createEntry(Roster.java:283)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at  com.wisechatter.Contact_ListActivity.addEntry(Contact_ListActivity.java:176)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at co  m.wisechatter.Contact_ListActivity.onClick(Contact_ListActivity.java:164)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.view.View.performClick(View.java:4204)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.view.View$PerformClick.run(View.java:17355)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at and roid.os.Handler.handleCallback(Handler.java:725)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.os.Handler.dispatchMessage(Handler.java:92)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.os.Looper.loop(Looper.java:137)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at  android.app.ActivityThread.main(ActivityThread.java:5041)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at   java.lang.reflect.Method.invokeNative(Native Method)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at  java.lang.reflect.Method.invoke(Method.java:511)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   03-31 12:34:19.770: E/AndroidRuntime(4886):  at dalvik.system.NativeStart.main(Native Method)

我知道这是一个非常愚蠢的问题,但我没有得到适当的解决方案..,请帮助我..,谢谢提前.. :)

1 个答案:

答案 0 :(得分:3)

只需在此处更改您的add_account行

  private void addEntry(String userName) 
{
    try {
        Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);

        String jid=userName+Constants.ADVANTLPC__SMACK;
        commanInstance.roster.createEntry(jid, jid  , null);
        Presence pres = new Presence(Presence.Type.subscribe);
        pres.setFrom(jid);
        commanInstance.connection.sendPacket(pres); 

        commanInstance.loadFriendList();

    } catch (Exception e) {
        Log.e("tag", "unable to add contact: ", e);
    }

}

}

并在loadfriendlist()这里将是你的列表视图。