我使用aSmack和Openfire数据库制作聊天应用程序。现在我因为NullPointerException而堆叠,这是在我尝试登录时发生的。有人可以帮我解决这个问题吗?我真的不知道该怎么做。 导致连接,loginUser,密码不为空。这些信息来自我的logcat。
org.jivesoftware.smack.tcp.XMPPTCPConnection@42c4fca0连接
AJ1 loginUser
密码54密码
我的代码:
public void connect() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
XMPPTCPConnectionConfiguration.Builder ConnectionConfiguration = XMPPTCPConnectionConfiguration.builder();
ConnectionConfiguration.setServiceName("lucky");
ConnectionConfiguration.setHost("192.168.100.5");
ConnectionConfiguration.setPort(5222);
ConnectionConfiguration.setSecurityMode(org.jivesoftware.smack.ConnectionConfiguration.SecurityMode.disabled);
ConnectionConfiguration.setDebuggerEnabled(true);
connection = new XMPPTCPConnection(ConnectionConfiguration.build());
XMPPConnectionListener xmppConnectionListener = new XMPPConnectionListener();
connection.addConnectionListener(xmppConnectionListener);
try {
connection.connect(); //Error here
} catch (SmackException.ConnectionException e) {
// e.printStackTrace();
Log.e("Point_1","Exception " + e);
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
t.start();
}
public class XMPPConnectionListener implements ConnectionListener {
@Override
public void connected(XMPPConnection connection) {
if(!connection.isAuthenticated()){
login(); //Error here
Log.e("Point_login", "XMPPListener, login()");
}
connected = true;
Log.e("Point_login", "XMPPListener, connected()");
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
loggedin = true;
Log.e("Point_login", "XMPPListener, authenticated");
}
@Override
public void connectionClosedOnError(Exception e) {
Log.e("Point_login", "XMPPListener, connectinClosedOnError " + e);
loggedin = false;
connected = false;
isChatCreated = false;
}
@Override
public void connectionClosed() {
loggedin = false;
connected = false;
isChatCreated = false;
Log.e("Point_login", "XMPPListener, connectionClosed");
}
@Override
public void reconnectingIn(int seconds) {
loggedin = false;
Log.e("Point_login", "XMPPListener, reconnectingIn");
}
@Override
public void reconnectionFailed(Exception e) {
loggedin = false;
connected = false;
isChatCreated = false;
Log.e("Point_login", "XMPPListener, reconnectionFailed");
}
@Override
public void reconnectionSuccessful() {
loggedin = false;
connected = true;
isChatCreated = false;
Log.e("Point_login", "XMPPListener, reconnectionSuccessful");
}
}
public void login(){
try{
Log.e("Point_login", String.valueOf(connection) + " connection");
Log.e("Point_login", String.valueOf(loginUser) + " loginUser");
Log.e("Point_login", String.valueOf(password) + " password");
connection.login(loginUser,password); //Error here
}catch(SmackException.NotConnectedException e){
Log.e("Login_1","Exception");
for(int i=0;i<=5;i++){
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}).start();
}
}catch(XMPPException | SmackException | IOException e){
e.printStackTrace();
Log.e("Login_1","2nd Exception");
}
}
我的logcat:
E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-2634
java.lang.NullPointerException
at org.jivesoftware.smack.util.stringencoder.Base64.encode(Base64.java:64)
at org.jivesoftware.smack.util.stringencoder.Base64.encode(Base64.java:60)
at org.jivesoftware.smack.util.stringencoder.Base64.encodeToString(Base64.java:42)
at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:199)
at org.jivesoftware.smack.sasl.SASLMechanism.authenticate(SASLMechanism.java:169)
at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:236)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginNonAnonymously(XMPPTCPConnection.java:365)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:452)
at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:427)
at com.lets.dothis.myapplication.XMPP.login(XMPP.java:180)
at com.lets.dothis.myapplication.XMPP$XMPPConnectionListener.connected(XMPP.java:206)
at org.jivesoftware.smack.AbstractXMPPConnection.callConnectionConnectedListener(AbstractXMPPConnection.java:1152)
at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:841)
at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:360)
at com.lets.dothis.myapplication.XMPP$1.run(XMPP.java:150)
at java.lang.Thread.run(Thread.java:856)
谢谢。
答案 0 :(得分:0)
在login()方法中传递“XMPPConnection”对象,即“连接”,如登录(连接)
然后,IN .... connection.login(loginUser,password); 用
替换该行if(!connection.isAuthenticated())
login(connection, loginUser, passwordUser);
else connection.login(loginUser, passwordUser);
我希望它能奏效。 请不要与两个具有相同名称的对象发生冲突 XMPPConnection&amp; XMPPTCPConnection,为简单起见,只需重命名一个。
<强>全球化志愿服务青年强>
http://xmpp-tutorials.blogspot.com.br/
请试试&amp;如果还有任何例外,请告诉我。谢谢