我在本地主機上運行 openfire 服務器,Smack 版本 4.2.4.GetAllContacts() 方法中的異常。下面是我的類 XmppConnection 的代碼,它擴展了類 org.jivesoftware.smack.ConnectionListener。 public class XmppConnection implements ConnectionListener{ private XMPPTCPConnection mConnection; public List<Contact> getAllContacts() throws SmackException.NotLoggedInException, InterruptedException, SmackException.NotConnectedException { Log.d(TAG,"getAllContats called()"); List<Contact> contacts = new ArrayList<>(); if(XmppConnectService.sConnectionState == ConnectionState.AUTHENTICATED){ Roster roster = Roster.getInstanceFor(mConnection); //exception here Collection<RosterEntry> entries = roster.getEntries(); Presence presence; for (RosterEntry entry : entries) { Contact c = new Contact(entry.getJid().toString()); presence = roster.getPresence(entry.getJid()); contacts.add(c); } return contacts;}public XmppConnection( Context context){ mApplicationContext = context.getApplicationContext(); String jid = PreferenceManager.getDefaultSharedPreferences(mApplicationContext) .getString("xmpp_jid",null); mPassword = PreferenceManager.getDefaultSharedPreferences(mApplicationContext) .getString("xmpp_password",null); if( jid != null) { mUsername = jid.split("@")[0]; mServiceName = jid.split("@")[1]; }else { mUsername =""; mServiceName=""; }}上面的代碼在 logcat 中顯示了這個錯誤:java.lang.NullPointerException: XMPPConnection must not be null我從另一個類調用 getAllContacts() 作為XmppConnection xmpp = new XmppConnection(this);contacts = xmpp.getAllContacts();getAllContacts() 方法是從另一個活動調用的,并在 authenticated() 方法之后調用。因為它是在 authenticated() 方法之后調用的,所以 mConnection 應該已經初始化了。
添加回答
舉報
0/150
提交
取消