我在某些情况下得到空指针异常。当应用程序进入前台时重新连接连接时会发生这种情况,因为我断开了背景上的xmpp连接以节省电池。
崩溃报告中的错误堆栈跟踪是:
java.util.Collections$UnmodifiableCollection.<init> (Collections.java:1099)
java.util.Collections$UnmodifiableSet.<init> (Collections.java:1203)
java.util.Collections.unmodifiableSet (Collections.java:1193)
org.jivesoftware.smack.ConnectionConfiguration.getEnabledSaslMechanisms (ConnectionConfiguration.java:476)
org.jivesoftware.smack.SASLAuthentication.selectMechanism (SASLAuthentication.java:359)
org.jivesoftware.smack.SASLAuthentication.authenticate (SASLAuthentication.java:191)
org.jivesoftware.smack.tcp.XMPPTCPConnection.loginInternal (XMPPTCPConnection.java:385)
org.jivesoftware.smack.AbstractXMPPConnection.login (AbstractXMPPConnection.java:491)
org.jivesoftware.smack.AbstractXMPPConnection.login (AbstractXMPPConnection.java:448)
rnxmpp.service.XmppServiceSmackImpl$1.doInBackground (XmppServiceSmackImpl.java:133)
rnxmpp.service.XmppServiceSmackImpl$1.doInBackground (XmppServiceSmackImpl.java:115)
android.os.AsyncTask$2.call (AsyncTask.java:316)
java.util.concurrent.FutureTask.run (FutureTask.java:237)
android.os.AsyncTask$SerialExecutor$1.run (AsyncTask.java:255)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
java.lang.Thread.run (Thread.java:776)
这是来自android代码的片段。
public void connect() {
if (connection == null) {
throw new RuntimeException("Cannot connect no connection!");
}
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
/*
* Normally, the reconnection logic would be handled by
* the ReconnectionManager, but it seems that this does
* not work reasonably well on Android. Therefore, we
* provide this naive implementation ourselves.
*
* See https://stackoverflow.com/a/38178025
*/
if (!connection.isConnected()) {
connection.connect();
}
if (!connection.isAuthenticated()) {
connection.login();
}
} catch (InterruptedException| XMPPException | SmackException | IOException e) {
logger.log(Level.SEVERE, "Could not login user", e);
if (e instanceof SASLErrorException) {
XmppServiceSmackImpl.this.xmppServiceListener.onLoginError(((SASLErrorException) e).getSASLFailure().toString());
} else {
XmppServiceSmackImpl.this.xmppServiceListener.onError(e);
}
}
return null;
}
@Override
protected void onPostExecute(Void dummy) {
}
}.execute();
}