Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask and IllegalArgumentException

in the app I am developing, when I touch the screen of the device, I connect to a server and i show a busy indicator using AsyncTask, and at this step i have no problem. but while i am connected and press the home buttom "the App goes into onPause" and i bring the App to be visible again "onResume" and touch the screen to connect to the server again, I receive the below posted logCat errors.

to note: in onPause I urnRegister the WiFi receiver and I disconnect from the server. and the logCat output shows the sequence of the invoked callbacks of the AsyncTask

I do not know why I am receiving IllegalArgumentException, i read some postes about it and I tested the object that is called "client", and it is never null

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;
    }
}
like image 625
rmaik Avatar asked Mar 09 '15 13:03

rmaik


1 Answers

I think the first thing to learn from this is to read ErrorLogs completely and understand it. The first line of the stacktrace says that

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)

i.e the error starts in MqttService class in the method getConnection(). The method getConnection() is throwing IllegalArgumentException and not the AsyncTask. It just happens to be called from inside AsyncTask. Also the variable ClientHandle is not part of Android API, it is part of your MqttService. This variable is actually invalid and is leading to the exception.

You can also share your MqttService class if you cannot figure out why the variable ClientHandle is Invalid. Maybe people here can assist you on that, but with the information provided this is what I can answer. Hope this helps.

like image 65
Roadblock Avatar answered Sep 28 '22 11:09

Roadblock