nell'app che sto sviluppando, quando tocco lo schermo del dispositivo, mi collego ad un server e mostro un indicatore occupato usando AsyncTask, ea questo punto non ho il problema . ma mentre sono connesso e premo il tasto home "l'App va in onPause" e porto l'App a essere nuovamente visibile "onResume" e tocchi lo schermo per collegarmi nuovamente al server, ricevo gli errori logCat riportati di seguito.AsyncTask e IllegalArgumentException
Nota: in onPausa IuroraRegistro il ricevitore WiFi e mi disconnetto dal server. e l'output logCat mostra la sequenza dei callback richiamati di AsyncTask
Non so perché sto ricevendo IllegalArgumentException, ho letto alcuni post su di esso e ho testato l'oggetto che si chiama "client", e non è mai nullo
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;
}
}
hai trovato una soluzione? – dmSherazi
Il problema non è il client né NullPointerException (il client è nullo), l'errore inizia in questa classe: MqttService, nella riga 552 e continua su MqttAndroidClient. Ti suggerisco di dare un'occhiata a – Chisko
@rmaik. Sto anche affrontando lo stesso problema, hai trovato qualche soluzione per questo? – Andy