QUICKBLOX Attempt to invoke virtual method sendMes

2019-08-01 03:20发布

问题:

i am trying to send text message in private dialog, everything works fine until i create private dialog but when i send text message then i get this error "Attempt to invoke virtual method 'void com.quickblox.chat.QBAbstractChat.sendMessage(com.quickblox.chat.model.QBChatMessage)' on a null object reference"

@Override
protected void onCreate(Bundle savedInstanceState) {

    final Context context = this;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    QBSettings.getInstance().init(getApplicationContext(), APP_ID, AUTH_KEY, AUTH_SECRET);
    QBSettings.getInstance().setAccountKey(ACCOUNT_KEY);

    QBChatService.setDebugEnabled(true);
    final QBChatService chatService = QBChatService.getInstance();
    final QBUser qbUser = new QBUser("shahzeb", "shahzeb143");
    QBAuth.createSession(qbUser).performAsync(new QBEntityCallback<QBSession>() {
        @Override
        public void onSuccess(QBSession qbSession, Bundle bundle) {
            qbUser.setId(qbSession.getUserId());


            chatService.login(qbUser, new QBEntityCallback() {
                @Override
                public void onSuccess(Object o, Bundle bundle) {


                    final QBChatDialog dialog = DialogUtils.buildPrivateDialog(25024405);
                    QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() {
                        @Override
                        public void onSuccess(QBChatDialog qbChatDialog, Bundle bundle) {

                            QBChatMessage chatMessage = new QBChatMessage();
                            chatMessage.setSenderId(qbUser.getId());
                            chatMessage.setBody("Hi there!");
                            try {
                                dialog.sendMessage(chatMessage);
                            } catch (SmackException.NotConnectedException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(QBResponseException e) {

                        }
                    });
                }

                @Override
                public void onError(QBResponseException e) {

                }
            });
        }

        @Override
        public void onError(QBResponseException e) {

        }
    });

}

}

回答1:

you have to call method sendMessage(..) on 'qbChatDialog' model not 'dialog' or you have to init 'dialog' for chat using dialog.initForChat(QBChatService.getInstance()); before sending message. See QuickBlox documentation