I have a project that uses a lot of asynchronous class AsyncTask to deal with, but all of these classes onPostExecute methods will not be executed, as well as on many models run about 4.0 have this problem, but in a system 4.0.3 run no problem. I do not know why, suspected environmental problems the project has met counterparts to pointing 12.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Landlord best code stickers out.
Reply:
No specific code, the situation is such a case.
Reply:
After the call is asynchronous class onPreExecute () doInBackground () method executes finished, onPostExecute () is not executed.
Reply:
No code can not locate
Reply:
To give you an asynchronous download pictures demo.
public class DownloadImageTask extends AsyncTask{
private Context mContext; / / reference to the calling Activity
int progress = -1;
Bitmap downloadedImage = null;
DownloadImageTask (Context context) {
mContext = context;
}
/ / Called from main thread to re-attach
protected void setContext (Context context) {
mContext = context;
if (progress> = 0) {
publishProgress (this.progress);
}
}
protected void onPreExecute () {
progress = 0;
/ / We could do some other setup work here before doInBackground () runs
}
protected Bitmap doInBackground (String. .. urls) {
Log.v ("doInBackground", "doing download of image ...");
return downloadImage (urls);
}
protected void onProgressUpdate (Integer. .. progress) {
TextView mText = (TextView)
((Activity) mContext) findViewById (R.id.text);.
mText.setText ("Progress so far:" + progress [0]);
}
protected void onPostExecute (Bitmap result) {
if (result! = null) {
downloadedImage = result;
setImageInView ();
}
else {
TextView errorMsg = (TextView)
((Activity) mContext) findViewById (R.id.errorMsg);.
errorMsg.setText ("Problem downloading image Please try later..");
}
}
public Bitmap downloadImage (String. .. urls)
{
HttpClient httpClient = CustomHttpClient.getHttpClient ();
try {
HttpGet request = new HttpGet (urls [0]);
HttpParams params = new BasicHttpParams ();
HttpConnectionParams.setSoTimeout (params, 60000); / / 1 minute
request.setParams (params);
setProgress (25);
HttpResponse response = httpClient.execute (request);
setProgress (50);
sleepFor (5000); / / five second sleep
byte [] image = EntityUtils.toByteArray (response.getEntity ());
setProgress (75);
Bitmap mBitmap = BitmapFactory.decodeByteArray (image, 0, image.length);
setProgress (100);
return mBitmap;
} Catch (IOException e) {
/ / Covers:
/ / ClientProtocolException
/ / ConnectTimeoutException
/ / ConnectionPoolTimeoutException
/ / SocketTimeoutException
e.printStackTrace ();
}
return null;
}
private void setProgress (int progress) {
this.progress = progress;
publishProgress (this.progress);
}
protected void setImageInView () {
if (downloadedImage! = null) {
ImageView mImage = (ImageView)
((Activity) mContext) findViewById (R.id.image);.
mImage.setImageBitmap (downloadedImage);
}
}
private void sleepFor (long msecs) {
try {
Thread.sleep (msecs);
} Catch (InterruptedException e) {
Log.v ("sleep", "interrupted");
}
}
}
Reply:
See if they have not encountered before, the code is no problem, no problem with other projects, this is the only project where all the Task class has this problem, there is no problem before dropping. Estimated that the project of the Environment, 4th Floor cited
Reply:
Before experiencing the same problem ~ ~ ~ brother for a solution to a seemingly loaded asynchronously loaded asynchronously android 3-4 ways Handler Runnable ==
Reply:
Find the cause of the problem, the problem is due to call this a step task in the service class caused.
Reply:
I had a similar problem, we help: http://topic.csdn.net/u/20120514/10/87c07b17-385a-4b73-a789-18bed8a16630.html?83984
Reply:
No comments:
Post a Comment