在与Smack 4.2重新连接后发送离线消息时,在经过身份验证的侦听器中出现异常

时间:2018-02-03 18:57:06

标签: java android xmpp chat smack

假设用户在聊天时丢失了连接,但他发送了一些消息。成功重新连接后,聊天应用程序需要发送未发送的消息。 我使用此代码发送Smack连接配置中配置的消息

private void sendMessage(String body, String toJid) throws XmppStringprepException {
    EntityBareJid jid = null;
    try {
        jid = JidCreate.entityBareFrom("monim@blah.im");
    } catch (XmppStringprepException e) {
        e.printStackTrace();
    }
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    Chat chat = chatManager.chatWith(jid);
    try {
        Message message = new Message(jid, Message.Type.chat);
        message.setBody(body);
        chat.send(message);
    }catch (SmackException.NotConnectedException | InterruptedException e) {
        e.printStackTrace();
    }
}

,这个用于从Messages活动

调用sendMessage()方法
private void sendMessage(String message, String receiverJid) {

    if (SmackConnectionService.getConnectionState().equals(SmackConnection.ConnectionState.CONNECTED)) {

            Intent intent1 = new Intent(SmackConnectionService.SEND_MESSAGE);
            intent1.putExtra(SmackConnectionService.BUNDLE_MESSAGE_BODY, message);
            intent1.putExtra(SmackConnectionService.BUNDLE_TO, receiverJid);
            DefaultMessagesActivity.this.sendBroadcast(intent1);
    } else {
        saveInDatabases();
        Toast.makeText(DefaultMessagesActivity.this, "Offline! Message not sent!", Toast.LENGTH_LONG).show();
    }
}

这可以正常工作,直到我断开互联网然后重新连接它。在尝试重新连接成功后调用sendMessage()方法从数据库发送未发送的消息时,会发生此错误

E/AbstractXMPPConnection: Exception in authenticated listener
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
 at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:396)
 at com.chatmate.user.chatapp.DefaultMessagesActivity.sendMessage(DefaultMessagesActivity.java:113)
 at com.chatmate.user.chatapp.DefaultMessagesActivity.sendMessageAgain(DefaultMessagesActivity.java:146)
 at com.chatmate.user.chatapp.SmackConnection.showContactListActivityWhenAuthenticated(SmackConnection.java:272)
 at com.chatmate.user.chatapp.SmackConnection.authenticated(SmackConnection.java:229)
 at org.jivesoftware.smack.AbstractXMPPConnection.callConnectionAuthenticatedListener(AbstractXMPPConnection.java:1228)
 at org.jivesoftware.smack.AbstractXMPPConnection.afterSuccessfulLogin(AbstractXMPPConnection.java:572)
 at org.jivesoftware.smack.tcp.XMPPTCPConnection.afterSuccessfulLogin(XMPPTCPConnection.java:377)
 at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginInternal(XMPPTCPConnection.java:395)
 at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:491)
 at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:448)
 at org.jivesoftware.smack.ReconnectionManager$2.run(ReconnectionManager.java:254)
 at java.lang.Thread.run(Thread.java:818)

其中错误DefaultMessagesActivity.java:113表示DefaultMessagesActivity.this.sendBroadcast(intent1);此行。

1 个答案:

答案 0 :(得分:0)

private void sendMessage(String message, String receiverJid)正在调用正在实施ConnectionListener的类。恢复connected()后调用XMPPTCPConnection方法,之后调用authenticated()。问题是,当我尝试在sendMessage中调用DefaultMessagesActivity.class时调用了行DefaultMessagesActivity.this.sendBroadcast(intent1);。但是这一行是从@overwrite方法调用的,它在Authenticated Listener中导致NULL指向异常。当我试图测试DefaultMessagesActivity是否为空时,我发现它从来都不是空的。但是,在此对象中广播对Authenticated Listener没有帮助。

  

解决方案

如果我必须调用sendMessage()方法发送离线消息,那么我应该在包含@overwrite方法connected()的文件中编写单独的代码。对于广播,我们使用Context.sendBroadcast(intent1);