1
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

JimmyHo
  • 279
  • 1
  • 5
  • 14
  • *Only the original thread that created a view hierarchy can touch its views*--it means that you manipulate UI outside UI thread, which is not allowed. Use `runOnUiThread()`: http://stackoverflow.com/questions/11140285/how-to-use-runonuithread – Alex Salauyou Mar 10 '16 at 09:52
  • probably something happens in your setBMP which shouldn't and that probably updating a view – Pooya Mar 10 '16 at 09:54
  • You cant access UI in doInBackground() method, your setBmp() accesses UI, then the error is raised by this. You can do UI work after task is completed in onPostExecuteMethod(), or call UI Thread as runOnUiThread(YOUR_THREAD). – Talha Mar 10 '16 at 10:10

3 Answers3

2

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.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
1

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
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
-2

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

Thanks

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37