AsyncTask和IllegalArgumentException

时间:2015-03-09 13:43:11

标签: android android-asynctask mqtt

在我正在开发的应用程序中,当我触摸设备的屏幕时,我连接到服务器并使用AsyncTask显示忙碌的指示器,并且在此步骤中我没有 问题。但是当我连接并按下家庭按钮时,应用程序进入onPause"然后我再次看到应用程序" onResume"并触摸屏幕 再次连接到服务器,我收到以下发布的logCat错误。

注意:在onPause我urnRegister WiFi接收器,我断开与服务器的连接。 logCat输出显示AsyncTask

的调用回调序列

我不知道为什么我收到IllegalArgumentException,我读了一些关于它的帖子,我测试了名为" client"的对象,它永远不会为空

logcat的

03-09 14:26:13.413: D/MainActivity(17065): @MQTTAsynchTask(): constructor called
03-09 14:26:13.413: D/MainActivity(17065): @MQTTAsynchTask(): client is not null
03-09 14:26:13.413: D/MainActivity(17065): @MQTTAsynchTask(): onPreExecute().
03-09 14:26:13.422: D/MainActivity(17065): @MQTTAsynchTask(): doInBackground().
03-09 14:26:13.433: E/AndroidRuntime(17065): FATAL EXCEPTION: pool-1-thread-1
03-09 14:26:13.433: E/AndroidRuntime(17065): Process: com.example.mqtt_designlayout_02, PID: 17065
03-09 14:26:13.433: E/AndroidRuntime(17065): java.lang.IllegalArgumentException: Invalid ClientHandle
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttService.getConnection(MqttService.java:552)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttService.connect(MqttService.java:318)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttAndroidClient.doConnect(MqttAndroidClient.java:427)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttAndroidClient.access$2(MqttAndroidClient.java:417)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttAndroidClient$1.run(MqttAndroidClient.java:395)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at java.lang.Thread.run(Thread.java:818)

AsynchTask

public MQTTAsynchTask(Context contex, MqttAndroidClient client, MqttConnectOptions opts) {
        // TODO Auto-generated constructor stub
        Log.d(TAG, "@MQTTAsynchTask(): constructor called");
        this.context = contex;
        this.MQTTAndroidClient = client;
        if (client != null) {
            Log.d(TAG, "@MQTTAsynchTask(): client is not null");
        }
        this.opts = opts;
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        Log.d(TAG, "@MQTTAsynchTask(): onPreExecute().");

        dialog = new Dialog(this.context);
        dialog.setCancelable(false);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progressdialog);
        progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        Log.d(TAG, "@MQTTAsynchTask(): doInBackground().");
        do {
            try {
                this.MQTTAndroidClient.connect(this.opts, this.context, synchCONNCallBack);
            } catch (MqttSecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MqttException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                Thread.sleep(MQTT_BROKER_TIME_OUT);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            this.totalTimeOut += MQTT_BROKER_TIME_OUT;

        } while ( (!this.isCancelled()) && (this.MQTTAndroidClient != null) && (!this.MQTTAndroidClient.isConnected()) && (this.totalTimeOut <= (10 * MQTT_BROKER_TIME_OUT)) );

        return null;
    }

的onPause

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.w(TAG, "@onPause()");

    this.onPauseCalled = true;

    if (reconnectTimerCurrentlyActive) {
        reconnectTimerCurrentlyActive = false;
        reconnectTimer.cancel();
        reconnectTimer.purge();
        Log.v(TAG, "reconnect timer set to 'false', and reconnectTimer is cancelled");
    }

    if (this.MQTTAsynch != null) {
        Log.d(TAG, "asynchTask object was initilised");
        if (this.MQTTAsynch.getStatus() == AsyncTask.Status.PENDING) {
            Log.d(TAG, "AsynchTask has not started yet.");
            boolean cancelledSuccessully = this.MQTTAsynch.cancel(true);
            if (cancelledSuccessully) {
                Log.d(TAG, "AsynchTask is cancelled successfully.");
            } else {
                Log.d(TAG, "AsynchTask failed to cancell");
            }
        }
        if (this.MQTTAsynch.getStatus() == AsyncTask.Status.RUNNING) {
            Log.d(TAG, "AsynchTask still running doing work in the backgroung thread, and it will be intrrupted");
            boolean cancelledSuccessully = this.MQTTAsynch.cancel(true);
            if (cancelledSuccessully) {
                Log.d(TAG, "AsynchTask is cancelled successfully.");
            } else {
                Log.d(TAG, "AsynchTask failed to cancell");
            }
        }
        if (this.MQTTAsynch.getStatus() == AsyncTask.Status.FINISHED) {
            Log.d(TAG, "AsynchTask has finished its work.");
        }
    } else {
        Log.d(TAG, "asynchTask object was not initilised. this.MQTTAsynch == null");
    }

    if ( (this.subActivityReturned) || (this.isConnectCalled) ) {
        MQTT_Disconnect_Module();
    }

    if (MQTTPrimaryReceiverRegistered) {
        unregisterReceiver(MQTTPrimaryReceiver);
        Log.v(TAG, "BroadCastReceiver (MQTTPrimaryReceiver) unregistered");
        MQTTPrimaryReceiverRegistered = false;
    }
}

1 个答案:

答案 0 :(得分:10)

我认为首先要学习的是完全阅读ErrorLogs并理解它。 stacktrace 的第一行表示

03-09 14:26:13.433: E/AndroidRuntime(17065): java.lang.IllegalArgumentException: Invalid ClientHandle
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttService.getConnection(MqttService.java:552)

即错误在方法MqttService中的getConnection()类中开始。方法getConnection()正在抛出IllegalArgumentException而不是AsyncTask。恰好从AsyncTask内部调用它。此外,变量 ClientHandle 不是Android API的一部分,它是MqttService的一部分。该变量实际上是无效的,并且导致异常。

如果您无法弄清楚变量MqttService无效的原因,您也可以共享ClientHandle课程。也许这里的人可以帮助你,但是根据所提供的信息,这是我能回答的问题。希望这会有所帮助。