简体   繁体   中英

Android AsyncTask sending message to a Handler on a dead thread

I am trying to make a program that should work something like a catalog - I have a JSON array that I loop through and parse the JSON objects into a string containing the image link, and two strings for descriptions for each object. Now, when downloading the images from the internet with the link, I run into a problem at the same image every time, image number 93. I checked the link, and it's working, just the same as the others. This happens:

 W/MessageQueue: Handler (android.os.Handler) {f95f6fe} sending message to a Handler on a dead thread
 java.lang.IllegalStateException: Handler (android.os.Handler) {f95f6fe} sending message to a Handler on a dead thread
     at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
     at android.os.Handler.enqueueMessage(Handler.java:643)
     at android.os.Handler.sendMessageAtTime(Handler.java:612)
     at android.os.Handler.sendMessageDelayed(Handler.java:582)
     at android.os.Handler.post(Handler.java:338)
     at android.os.ResultReceiver$MyResultReceiver.send(ResultReceiver.java:57)
     at com.android.internal.os.IResultReceiver$Stub.onTransact(IResultReceiver.java:58)
     at android.os.Binder.execTransact(Binder.java:565)

This is how my AsyncTask looks:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

This is how I call the AsyncTask:

 new DownloadImageTask(imgProductPicture).execute(pictureLink);
  layoutTest.addView(NewImgView);

In my code I make sure that no more than 5 of these are called at a time, and they all load without issue until reaching the object indexed 92. The object itself is fine, but the app shuts off at this point always. Does anyone have an idea why? I've tried downloading less images at a time (one by one) and it still fails at the same point. I'd appreciate any help.

AsyncTask uses handler of the main thread, to callback onPostExecute() . If the main thread is dead when to callback, system throws the exception. To avoid this, you have to keep the main thread alive until all the work completes.

I solved the problem by creating a new handler/runnable every time I called the DownloadImageTask. Thanks to all who have tried to help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM