QUICKBLOX Attempt to invoke virtual method sendMessage on null object reference

六眼飞鱼酱① 提交于 2019-12-11 07:36:34

问题


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



来源:https://stackoverflow.com/questions/42738236/quickblox-attempt-to-invoke-virtual-method-sendmessage-on-null-object-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!