2016-03-10 70 views
0
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; 
    } 

我删除了一些代码并不重要的AsyncTask ---在执行doInBackground()

我想做

出现错误 - > setBmp(link_list);

  • link_list是一个字符串数组内容的URL(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

+0

*只有创建视图层次结构的原始线程可以触及其视图* - 这意味着您在UI线程之外操作UI,这是不允许的。使用'runOnUiThread()':http:// stackoverflow。com/questions/11140285/how-to-use-runonuithread –

+0

可能在你的setBMP中发生了什么,它不应该和可能更新视图 – Pooya

+0

你无法访问doInBackground()方法中的UI,你的setBmp()访问UI,然后由此提出错误。您可以在onPostExecuteMethod()中完成任务后执行UI工作,或者将UI线程调用为runOnUiThread(YOUR_THREAD)。 – Talha

回答

-2

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

感谢

1

错误,因为setBmp()某种程度上影响的看法,这是不允许在其他线程不是UI线程。用runOnUiThread()包装它将有所帮助。替换行

setBmp(link_list); 

由:

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

要使其工作,你也应该做最后的创作link_list

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

它也是android.os.NetworkOnMainThreadException – JimmyHo

+0

不是,这是另一个例外。请在你的问题中发布'setBmp()'代码 –

1

依我之见,setBmp()方法调用UI更新。 UI更新应该在UI线程中进行。你应该知道doInBackground发生在后台线程和而不是的UI线程。

不要在doInBackground方法中调用它,请尝试拨打AsyncTaskonPostExecute方法中的setBmp()

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

相关问题