我试图演示AsyncTask以及Asynctask中方法的执行顺序。我已经在每种方法中使用Toast来查看订单。 以下是我使用的代码段:
private class UploadImage extends AsyncTask<Void, Void, Void> {
Bitmap image;
String name;
public UploadImage(Bitmap image, String name){
this.image = image;
this.name = name;
Toast.makeText(getApplicationContext(), "Constructor - UpImg",Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... arg0) {
Toast.makeText(getApplicationContext(), "DoinBgd - UpImg",Toast.LENGTH_SHORT).show();
/* ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
Toast.makeText(getApplicationContext(), "DoInBgd - UpImg",Toast.LENGTH_LONG).show();
ArrayList<NameValuePair> dataToSend = new ArrayList();
dataToSend.add(new BasicNameValuePair("image", encodedImage));
dataToSend.add(new BasicNameValuePair("name", name));
HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVERADDRESS + "/SavePicture.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch(Exception e)
{
e.printStackTrace();
}*/
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "OnPost exec - UpImg",Toast.LENGTH_SHORT).show();
super.onPostExecute(result);
//Toast.makeText(getApplicationContext(), "Image Uploaded" , Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "PreExec - UpImg",Toast.LENGTH_SHORT).show();
/*ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); */
super.onPreExecute();
}
}
其他方法的Toast正确显示。但是,当我在DoInBac ...()方法中放入Toast时,我收到错误。 以下是LogCat()输出:
05-17 10:38:51.888: E/AndroidRuntime(18508): FATAL EXCEPTION: AsyncTask #3
05-17 10:38:51.888: E/AndroidRuntime(18508): java.lang.RuntimeException: An error occured while executing doInBackground()
05-17 10:38:51.888: E/AndroidRuntime(18508): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-17 10:38:51.888: E/AndroidRuntime(18508): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-17 10:38:51.888: E/AndroidRuntime(18508): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-17 10:38:51.888: E/AndroidRuntime(18508): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-17 10:38:51.888: E/AndroidRuntime(18508): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-17 10:38:51.888: E/AndroidRuntime(18508): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-17 10:38:51.888: E/AndroidRuntime(18508): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-17 10:38:51.888: E/AndroidRuntime(18508): at java.lang.Thread.run(Thread.java:856)
05-17 10:38:51.888: E/AndroidRuntime(18508): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-17 10:38:51.888: E/AndroidRuntime(18508): at android.os.Handler.<init>(Handler.java:197)
05-17 10:38:51.888: E/AndroidRuntime(18508): at android.os.Handler.<init>(Handler.java:111)
05-17 10:38:51.888: E/AndroidRuntime(18508): at android.widget.Toast$TN.<init>(Toast.java:324)
05-17 10:38:51.888: E/AndroidRuntime(18508): at android.widget.Toast.<init>(Toast.java:91)
05-17 10:38:51.888: E/AndroidRuntime(18508): at android.widget.Toast.makeText(Toast.java:238)
05-17 10:38:51.888: E/AndroidRuntime(18508): at com.msrohit.imageselect.MainActivity$UploadImage.doInBackground(MainActivity.java:191)
05-17 10:38:51.888: E/AndroidRuntime(18508): at com.msrohit.imageselect.MainActivity$UploadImage.doInBackground(MainActivity.java:1)
RunTimeException背后的原因是什么?
答案 0 :(得分:0)
你无法触摸doinbackground上的UI线程,而是使用onPostExecute或runOnUiThread等方法,或者只是向句柄发送消息以从doInBackground中更新你的进度:
runOnUiThread(new Runnable() {
public void run() {
//Do something on UiThread
}
});