问题描述
我正在尝试在私人对话框中发送文本消息,一切正常,直到我创建私人对话框,但是当我发送文本消息时,我收到此错误尝试调用虚拟方法 'void com.quickblox.chat.QBAbstractChat.sendMessage(com.quickblox.chat.model.QBChatMessage)' 在空对象引用上"
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) {
}
});
}
}
推荐答案
您必须在 'qbChatDialog' 模型上调用方法 sendMessage(..) 而不是 'dialog' 或者您必须使用 dialog.initForChat 初始化 'dialog' 进行聊天(QBChatService.getInstance());在发送消息之前.请参阅 QuickBlox 文档
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
这篇关于QUICKBLOX 尝试在空对象引用上调用虚拟方法 sendMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!