【问题标题】:AsyncTask --- An error occurred while executing doInBackground()AsyncTask --- 执行 doInBackground() 时出错
【发布时间】:2016-03-10 09:48:38
【问题描述】:
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;
    }

我删除了一些代码 不重要

我想做

-> setBmp(link_list);

  • link_list 是一个字符串数组内容 urls (http://xxx.xx.jpg)

  • setbmp可以下载图片和设置imageview

现在有错误消息

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)

编辑

所以..

  1. 主线程不能做网络
  2. doInBackground 无法设置 UI

如何下​​载和更改用户界面

【问题讨论】:

  • 只有创建视图层次结构的原始线程才能接触到它的视图——这意味着你在 UI 线程之外操作 UI,这是不允许的。使用runOnUiThread():stackoverflow.com/questions/11140285/how-to-use-runonuithread
  • 可能在您的 setBMP 中发生了不应该发生的事情,并且可能会更新视图
  • doInBackground() 方法中无法访问 UI,setBmp() 访问 UI,则由此引发错误。你可以在 onPostExecuteMethod() 中完成任务后做 UI 工作,或者调用 UI Thread 作为 runOnUiThread(YOUR_THREAD)。

标签: java android multithreading android-asynctask


【解决方案1】:

如我所见,setBmp() 方法调用了 UI 更新。 UI 更新应该发生在 UI 线程中。您应该知道doInBackground 出现在后台线程中,而不是 UI 线程中。

不要在doInBackground 方法中调用它,而是尝试从AsyncTaskonPostExecute 方法调用setBmp()

注意:onPostExecute 方法在 UI 线程中执行。

【讨论】:

    【解决方案2】:

    发生错误是因为setBmp() 以某种方式影响视图,这在 UI 线程以外的其他线程上是不允许的。用runOnUiThread() 包裹它会有所帮助。换行

    setBmp(link_list);
    

    作者:

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

    要使其正常工作,您还应该在创建时将link_list 设为final:

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

    【讨论】:

    • 不是,这是另一个例外。请在您的问题中发布setBmp() 代码
    【解决方案3】:

    这是你的 setBmp(link_list); 方法。可能是这种方法尝试显示图像。 **doInBackground** 方法不创建视图。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多