MqttAndroidClient.publish()方法时空指针。
问题描述:安卓mqtt发送一条消息,发送之前检测网络,发现网络突然断了,又突然连上了,此时发送消息时,报空指针 mClient不设置为空的话 ,始终不为null
相关代码:
public void publish(String topic, String message) {
try {
InsideApplication.getSharedPreferences().getString(Const.ROOM_ID, "");
String roomTopic = topic;
if (!isNetworkConnected(InsideApplication.getApplication())) {
LogUtils.d(TAG, "publish()--没有网");
//在这里尝试过注销这mClient广播接收器 设置成null,
//这个网络断开和连接非常快设置了睡眠 也无用
}
if (mClient == null) {
mClient = new MqttAndroidClient(InsideApplication.getApplication(), mUri, DeviceUuidFactory.getLocalSerialHex());
mClient.setCallback(this);
//连接
}
mClient.publish(
roomTopic,
message.getBytes(),
MQTTConfig.QOS,
false,
null,
new ActionListener(
mContext,
Action.PUBLISH,
this));
} catch (MqttSecurityException e) {
} catch (MqttException e) {
}
报错信息:
Caused by: java.lang.NullPointerException
at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:715)
at com.insideroom.mqtt.MQTTConnect.publish(MQTTConnect.java:112)
at com.insideroom.base.InsideApplication.publish(InsideApplication.java:93)
at com.insideroom.activity.VoiceNameActivity.pushNET(VoiceNameActivity.java:224)
at com.insideroom.activity.VoiceNameActivity.onCreate(VoiceNameActivity.java:110)
at android.app.Activity.performCreate(Activity.java:5131)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请问下 这个问题解决了吗 MqttAndroidClient.publish()空异常