在我的应用程序中,我连接到 MQTT 服务器/代理,从 SQLiteDB 检索连接设置/配置。我检查了检索到的条目(ip、端口、clientID、KAtimer、sessionFlag)并且它们都有效。
这些配置被返回onActivityResult
,在onActivityResult
,我调用方法
private void setUpMQTTEnvironment(Bundle extras)
它没有在下面发布,它只是从从 subActivity 返回的捆绑包中提取值并将捆绑包中包含的每个值分配给其对应的值set() method
,例如,当我提取 IP 和端口时,我将它们添加到 setIP( ip) 和 setPORT(端口)
在onResume()
我调用MQTTConnect(...,..)
下面发布的连接到服务器时,实际上tp到这一步方法的同步回调connect
说connection fails
但这也意味着客户端对象不为空
问题是当我按下后退按钮时,onpause
被调用并且我收到NPE
如logcat errors
为什么我收到NPE
?我检查了客户对象是否为空,如果它不为空,我不应该收到NPE
错误,我是空的,应用程序应该正常关闭。
请帮我找出错误,为什么我会收到NPE
MQTT连接:
private void MQTTConnect(MqttAndroidClient client, MqttConnectOptions opts) throws MqttException {
// TODO Auto-generated method stub
client.registerResources(this);
client.setCallback(asynchCallBack);
client.connect(opts, getApplicationContext(), synchCallBack);
this.setClient(client);
}
恢复:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.w(TAG, "@onResume()");
if ( (this.MQTT_ASSETS_AVAILABLE) && (this.getClient() != null) && (this.getClientOpts() != null) ) {
Log.d(TAG, "@onResume(): ConnectionAssets: not null, Client: not null, opts: not null");
try {
MQTTConnect(this.getClient(), this.getClientOpts());
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(TAG, "@onResume: Exception catched -> Connection problems.");
}
} else {
Log.d(TAG, "@onResume(): either ConnectionAssets are not available or client/opts is null");
}
}
暂停:
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.w(TAG, "@onPause()");
if ((this.getClient() != null) ) {
if (this.getClient().isConnected()) {
Log.w(TAG, "@onPause(): client is not null");
this.getClient().close();
this.getClient().unregisterResources();
try {
this.getClient().disconnect(this, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken arg0) {
// TODO Auto-generated method stub
Log.w(TAG, "@onSuccess(): disconnection successfull");
}
@Override
public void onFailure(IMqttToken arg0, Throwable arg1) {
// TODO Auto-generated method stub
Log.w(TAG, "@onFailure(): disconnection failed");
}
});
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
LogCat 错误:
02-04 12:32:37.618: E/AndroidRuntime(10392): FATAL EXCEPTION: main
02-04 12:32:37.618: E/AndroidRuntime(10392): Process: com.example.mqtt_designlayout_02, PID: 10392
02-04 12:32:37.618: E/AndroidRuntime(10392): java.lang.RuntimeException: Unable to pause activity {com.example.mqtt_designlayout_02/com.example.mqtt_designlayout_02.MainActivity}: java.lang.NullPointerException
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3185)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3140)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3118)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.access$1000(ActivityThread.java:157)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1264)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.os.Handler.dispatchMessage(Handler.java:102)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.os.Looper.loop(Looper.java:157)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.main(ActivityThread.java:5293)
02-04 12:32:37.618: E/AndroidRuntime(10392): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 12:32:37.618: E/AndroidRuntime(10392): at java.lang.reflect.Method.invoke(Method.java:515)
02-04 12:32:37.618: E/AndroidRuntime(10392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-04 12:32:37.618: E/AndroidRuntime(10392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-04 12:32:37.618: E/AndroidRuntime(10392): at dalvik.system.NativeStart.main(Native Method)
02-04 12:32:37.618: E/AndroidRuntime(10392): Caused by: java.lang.NullPointerException
02-04 12:32:37.618: E/AndroidRuntime(10392): at org.eclipse.paho.android.service.MqttAndroidClient.isConnected(MqttAndroidClient.java:227)
02-04 12:32:37.618: E/AndroidRuntime(10392): at com.example.mqtt_designlayout_02.MainActivity.onPause(MainActivity.java:513)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.Activity.performPause(Activity.java:5493)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1251)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3171)