我有一个主要活动有2个AsyncTask,我希望其中一个AsyncTask
中止另一个AsyncTask
。
class Contador extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
httpclientContador = new DefaultHttpClient();
String sContador = "http://webmilab.com/SEDESOL/index.php/transferencias/conteo/" + resIdEncuesta;
pContador = new HttpPost(sContador);
pContador.addHeader("content-type", "application/x-www-form-urlencoded");
JSONObject jContador = new JSONObject();
jContador.put("mensaje", "Empezar a contar :)");
StringEntity eContador = new StringEntity("json="+jContador.toString(), "UTF-8");
pContador.setEntity(eContador);
httpclientContador.execute(pContador);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(TransferirDatos.this, "Hubo un error al realizar la petición del conteo", Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
}
}
}
这是我想要完成的AsyncTask。
class Contador extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
//Some Code
return null;
}
protected void onPostExecute(String file_url) {
pContador.abort();
}
}
HttpPost pContador
和HttpClient httpclientContador
是主要活动中定义的变量。
当我尝试执行此代码时,应用程序始终崩溃,我想知道如何从第二个AsyncTask
中止AsyncTask
。
这是Log。
04-08 09:22:48.625: E/AndroidRuntime(10950): FATAL EXCEPTION: AsyncTask #3
04-08 09:22:48.625: E/AndroidRuntime(10950): java.lang.RuntimeException: An error occured while executing doInBackground()
04-08 09:22:48.625: E/AndroidRuntime(10950): at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-08 09:22:48.625: E/AndroidRuntime(10950): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.lang.Thread.run(Thread.java:856)
04-08 09:22:48.625: E/AndroidRuntime(10950): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-08 09:22:48.625: E/AndroidRuntime(10950): at android.os.Handler.<init>(Handler.java:121)
04-08 09:22:48.625: E/AndroidRuntime(10950): at android.widget.Toast$TN.<init>(Toast.java:317)
04-08 09:22:48.625: E/AndroidRuntime(10950): at android.widget.Toast.<init>(Toast.java:91)
04-08 09:22:48.625: E/AndroidRuntime(10950): at android.widget.Toast.makeText(Toast.java:233)
04-08 09:22:48.625: E/AndroidRuntime(10950): at tsp.consulting.TransferirDatos$Contador.doInBackground(TransferirDatos.java:184)
04-08 09:22:48.625: E/AndroidRuntime(10950): at tsp.consulting.TransferirDatos$Contador.doInBackground(TransferirDatos.java:1)
04-08 09:22:48.625: E/AndroidRuntime(10950): at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-08 09:22:48.625: E/AndroidRuntime(10950): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
答案 0 :(得分:2)
您的问题是您正在尝试在doInBackground方法中打印Toast。 此方法在后台线程中执行,因此无法与UI交互。 你可以在这里阅读更多内容。
您可以使用onPostExecute(Result)方法在任务结束后通知用户,或在任务运行时更新onProgressUpdate(Progress...)。