简体   繁体   中英

AsyncTask — An error occurred while executing doInBackground()

protected String doInBackground(String... params) {
        int progress = 0;

        publishProgress(progress += 10);

        String[] link_list = new String[100];
        Bitmap bmp ;
        Document xmlDoc = Jsoup.parse(url, 3000);

            Elements title = xmlDoc.select("div[class=meta]");

            title_lenth = title.size();

            for (int i = 0; i < title_lenth; i++) {
                link_list[count] = title.get(i).text();
                try{
                    JSONObject JObj_link ;
                    JObj_link = new JSONObject(link_list[count]);
                    link_list[count] = JObj_link.getString("ou");
                    Log.e("ou Content", link_list[count]);
                }catch (Exception e){
                    Log.e("ou Content", e.toString());
                }
                    System.out.print(titlelist[count]);
                    count ++ ;
            }
            setBmp(link_list);
            publishProgress(progress += 15);

        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        publishProgress(progress = 100);

        return title_word;
    }

I deleted some code Not critical

I want to do

-> setBmp(link_list);

  • link_list is a string array content urls ( http://xxx.xx.jpg )

  • the setbmp can download pics and set imageview

now there is error messages

Logcat :

03-10 09:32:23.461  16218-16493/com.takepickpicturedemo E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.takepickpicturedemo, PID: 16218
    java.lang.RuntimeException: An error occurred while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:309)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
            at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:942)
            at android.view.ViewGroup.invalidateChild(ViewGroup.java:5081)
            at android.view.View.invalidateInternal(View.java:12713)
            at android.view.View.invalidate(View.java:12677)
            at android.view.View.invalidate(View.java:12661)
            at android.widget.AbsListView.resetList(AbsListView.java:1996)
            at android.widget.GridView.setAdapter(GridView.java:194)
            at com.takepickpicturedemo.MainActivity.setBmp(MainActivity.java:741)
            at com.takepickpicturedemo.MainActivity$GetPredict.doInBackground(MainActivity.java:582)
            at com.takepickpicturedemo.MainActivity$GetPredict.doInBackground(MainActivity.java:497)
            at android.os.AsyncTask$2.call(AsyncTask.java:295)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)

edit

so ..

  1. Main thread cannot do Network
  2. doInBackground cannot setting UI

How can I do to download & change UI

Error occurs because setBmp() somehow affects views, which is not allowed on other threads than UI thread. Wrappping it with runOnUiThread() will help. Replace the line

setBmp(link_list);

by:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        setBmp(link_list);
    }
});

To make it work, you also should make link_list final on creation:

final String[] link_list = new String[100];  // "final" added

As I see, setBmp() method calls a UI update. A UI update should occur in the UI thread. You should know that doInBackground occurs in a background thread and not the UI thread.

Instead of calling it in the doInBackground method, try calling setBmp() from onPostExecute method of the AsyncTask .

NOTE: onPostExecute method is executed in the UI thread.

Here is you setBmp(link_list); Method. May be this Method Try to show Image. **doInBackground** method do not created a view.

Thanks

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