我正在本地主机上运行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="";
}
}
public void connect() throws IOException,XMPPException,SmackException
{
Log.d(TAG, "Connecting to server " + mServiceName);
InetAddress addr = InetAddress.getByName(OPENFIRE_HOST);
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
DomainBareJid serviceName = JidCreate.domainBareFrom(OPENFIRE_HOST);
conf = XMPPTCPConnectionConfiguration.builder();
conf.setXmppDomain(serviceName
.setHostAddress(addr)
.setUsernameAndPassword(mUsername,mPassword)
.setPort(5222)
.setHostnameVerifier(verifier)
.setSecurityMode(ConnectionConfiguration.SecurityMode.required)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setCompressionEnabled(true);
mConnection = new XMPPTCPConnection(conf.build());
setupUiThreadBroadCastMessageReceiver();
mConnection.addConnectionListener(this);
try {
mConnection.connect();
mConnection.login(mUsername,mPassword);
} catch (InterruptedException e) {
e.printStackTrace();
}
ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
reconnectionManager.setEnabledPerDefault(true);
reconnectionManager.enableAutomaticReconnection();
}
}
@Override
public void connected(XMPPConnection connection) {
XmppConnectService.sConnectionState = ConnectionState.CONNECTED;
Log.w(TAG,"Connected Successfully; id:" + XmppConnectService.sConnectionState);
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
XmppConnectService.sConnectionState = ConnectionState.AUTHENTICATED;
Log.w(TAG,"Authenticated Successfully");
showContactListActivityWhenAuthenticated();
}
}
}
以上代码在logcat中显示此错误:java.lang.NullPointerException: XMPPConnection must not be null
我正在从另一个类中调用getAllContacts()
XmppConnection xmpp = new XmppConnection(this);
contacts = xmpp.getAllContacts();
getAllContacts()方法是从另一个活动中调用的,并在authenticated()方法之后调用。 由于它是在authenticated()方法之后调用的,因此mConnection应该已初始化。
答案 0 :(得分:0)
如代码所示,您可以在connect()方法中初始化mConnection变量。 而且您的authenticated()方法不会对connect()方法进行任何调用。 您能否确保在调用authenticated()方法之前或在方法内部调用方法connect()
答案 1 :(得分:0)
为XmppConnection类创建新对象会使用null初始化其变量。 将XMPPTCPConnection声明为静态可行:
private static XMPPTCPConnection mConnection;