AsyncTask and IllegalArgumentException

in the application that I am developing, when I touch the device screen, I connect to the server and I show a busy indicator using AsyncTask, and at this point I have no problem. but while I am connected and I press the home button "the application goes to onPause" and I bring the application again apparently "onResume" and touch the screen to reconnect to the server, I get the logCat errors below.

note: in onPause I am an urnRegister WiFi receiver and I will disconnect from the server. and logCat output shows AsyncTask callback sequence

I donโ€™t know why I get an IllegalArgumentException, I read a few posts about it and I tested an object called a โ€œclientโ€ and it never matters

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; } } 
+6
source share
1 answer

I think the first thing to learn from this is to fully read ErrorLogs and understand it. The first line of stacktrace indicates 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) 

In this case, the error begins with the MqttService class in the getConnection() method. The getConnection() method throws an IllegalArgumentException , not an AsyncTask . It just comes from within AsyncTask . Also, the ClientHandle variable is not part of the Android API, it is part of your MqttService . This variable is actually invalid and throws an exception.

You can also share your MqttService class if you cannot understand why the ClientHandle variable is ClientHandle valid. Perhaps people here can help you with this, but with the information provided, this is what I can answer. Hope this helps.

+9
source

Source: https://habr.com/ru/post/983533/


All Articles