在我的应用程序中,我正在使用AsynchTask来显示忙碌的指示器,我正在尝试连接到服务器。当应用程序进入onPause()时,我取消了AsynchTask 如下面的发布代码所示。当应用程序从onPause进入onResume时,当我尝试连接到服务器时,我希望AsynchTask能够被执行 显示繁忙的指标,但会发生的是,AsynchTask执行并显示bsy指标,但之后应用程序崩溃。此行为仅在发生时发生 当我来自onPause(onPause-> onResume)时,我尝试连接到服务器。
所以根据logCat输出,据我所知,当我试图取消AsynchTask时,我正在考虑usig处理程序 请让我知道如何解决这个问题。
我离开时收到以下LogCat:
03-09 16:51:27.024: E/AndroidRuntime(26111): FATAL EXCEPTION: pool-1-thread-1
03-09 16:51:27.024: E/AndroidRuntime(26111): Process: com.example.mqtt_designlayout_02, PID: 26111
03-09 16:51:27.024: E/AndroidRuntime(26111): java.lang.IllegalArgumentException: Invalid ClientHandle
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttService.getConnection(MqttService.java:552)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttService.connect(MqttService.java:318)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttAndroidClient.doConnect(MqttAndroidClient.java:427)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttAndroidClient.access$2(MqttAndroidClient.java:417)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttAndroidClient$1.run(MqttAndroidClient.java:395)
03-09 17:08:57.023: E/AndroidRuntime(26667): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 17:08:57.023: E/AndroidRuntime(26667): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 17:08:57.023: E/AndroidRuntime(26667): at java.lang.Thread.run(Thread.java:818)
有时候会有以下几点:
03-09 16:51:27.024: E/AndroidRuntime(26111): FATAL EXCEPTION: pool-1-thread-2
03-09 16:51:27.024: E/AndroidRuntime(26111): Process: com.example.mqtt_designlayout_02, PID: 26111
03-09 16:51:27.024: E/AndroidRuntime(26111): java.lang.IllegalArgumentException: Invalid ClientHandle
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttService.getConnection(MqttService.java:552)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttService.connect(MqttService.java:318)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttAndroidClient.doConnect(MqttAndroidClient.java:427)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttAndroidClient.access$2(MqttAndroidClient.java:417)
03-09 17:08:57.023: E/AndroidRuntime(26667): at org.eclipse.paho.android.service.MqttAndroidClient$1.run(MqttAndroidClient.java:395)
03-09 17:08:57.023: E/AndroidRuntime(26667): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 17:08:57.023: E/AndroidRuntime(26667): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 17:08:57.023: E/AndroidRuntime(26667): at java.lang.Thread.run(Thread.java:818)
代码:
private void cancelAsynchTask() {
// TODO Auto-generated method stub
Log.w(TAG, "@cancelAsynchTask().");
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");
}
}
doInBackGround :
@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) && (!isConnectCalled) && (!this.MQTTAndroidClient.isConnected()) && (this.totalTimeOut <= (10 * MQTT_BROKER_TIME_OUT)) );
return null;
}
答案 0 :(得分:1)
onPreExecute()
的{{1}}和onPostExecute()
在主UI线程上执行。这意味着您可以对UI进行任何更改,包括显示任何对话框。覆盖这些方法并同样修改UI。只有AsyncTask
在后台执行,并且对此UI的任何更改都要求您使用doInBackground()
。