2016-03-10 156 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線程中執行。

相關問題