Android onPause中的NPE,尽管对象是针对null进行测试的

Android onPause中的NPE,尽管对象是针对null进行测试的,android,mqtt,paho,Android,Mqtt,Paho,在我的应用程序中,我连接到MQTT服务器/代理,连接seetings/配置从SQLiteDB检索。我检查了检索到的条目(ip、端口、clientID、KAtimer、sessionFlag),并且所有条目都有效 这些配置在onActivityResult中返回,在onActivityResult中,我调用该方法 private void setUpMQTTEnvironment(Bundle extras)下面没有发布,而是从子活动返回的Bundle中提取值,并将Bundle中包含的每个值分配给

在我的应用程序中,我连接到MQTT服务器/代理,连接seetings/配置从SQLiteDB检索。我检查了检索到的条目(ip、端口、clientID、KAtimer、sessionFlag),并且所有条目都有效

这些配置在
onActivityResult
中返回,在
onActivityResult
中,我调用该方法

private void setUpMQTTEnvironment(Bundle extras)
下面没有发布,而是从子活动返回的Bundle中提取值,并将Bundle中包含的每个值分配给相应的
set()方法
,例如,当我提取IP和端口时,我将它们分配给setIP(IP)和setPORT(港口)

在下面发布的连接到服务器的
onResume()
i调用
MQTTConnect(…,…)
中,实际上将
connect
方法的同步回调称为
连接失败
,但这也意味着客户机对象不是空的

问题是当我按下后退按钮时,
onpause
被调用,我收到
NPE
,如
logcat errors

为什么我收到
NPE
?我检查了clien对象是否为null,如果它不是null,我不应该收到
NPE
错误,如果它为null,应用程序应该正常关闭

请帮助我找到错误,为什么我收到
NPE

MQTTConnect

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();
            }
        }
    }
}
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)
恢复时

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();
            }
        }
    }
}
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)
暂停时

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();
            }
        }
    }
}
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)
日志猫错误

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();
            }
        }
    }
}
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)

为什么要在onpause中发出网络请求?@Blackbelt isConnected()是我在项目中导入的paho库中的一个不正确的方法。我还没有开发isConnected()!!在方法的第一行上放置一个断点并逐步通过调试器。您可以检查所有内容以查看它是否为null。无需依靠猜测!@IllegalArgument否,我不是在onPause中请求连接,我在onResum中连接,如果(clientobject!=null)&&(clientobject.isconnected())我在onPausedo中断开客户端,您是否有服务标签“