2012-07-13 102 views
2

我正在使用FragmentActivity來交換兩個選項卡。這是選項卡A和選項卡B.在選項卡B我使用ListView與Loadmore按鈕,當假設加載更多按鈕點擊我只是稱爲backgroud線程。Android碎片和線程

private class loadMoreListView extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     // Showing progress dialog before sending http request 
     pDialog = new ProgressDialog (
       getActivity()); 
     pDialog.setMessage("Please wait.."); 
     pDialog.setIndeterminate(true); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    protected Void doInBackground(Void... unused) { 
     try { 
      //service url to fectch data.. 
      //xml parsing 
     } catch (Exception e) { 
      response = "failure"; 
      System.out.println(" sec XML Pasing Excpetion = " + e); 
     } 

     int currentPosition = listViewattlist.getFirstVisiblePosition(); 

     // Appending new data to menuItems ArrayList 
     adapter = new CustomAdapter(getActivity(), R.layout.att_list, R.id.title, data); 
     listViewattlist.setAdapter(adapter); 

     // Setting new scroll position 
     listViewattlist.setSelectionFromTop(currentPosition + 1, 0); 
     return (null); 
    } 

    protected void onPostExecute(Void unused) { 
     // closing progress dialog 
     pDialog.dismiss(); 
    } 
} 

當我換,並點擊該按鈕,我能看到進度條,但它顯示如下錯誤:

07-13 11:42:29.386: E/AndroidRuntime(894): FATAL EXCEPTION: AsyncTask #2 
07-13 11:42:29.386: E/AndroidRuntime(894): java.lang.RuntimeException: An error occured while executing doInBackground() 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.lang.Thread.run(Thread.java:856) 
07-13 11:42:29.386: E/AndroidRuntime(894): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.View.invalidate(View.java:10250) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.View.invalidate(View.java:10205) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ImageView.invalidateDrawable(ImageView.java:190) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:350) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.graphics.drawable.Drawable.setVisible(Drawable.java:546) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ImageView.onDetachedFromWindow(ImageView.java:1173) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.View.dispatchDetachedFromWindow(View.java:11789) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3819) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.AbsListView.resetList(AbsListView.java:1936) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ListView.resetList(ListView.java:502) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.widget.ListView.setAdapter(ListView.java:442) 

07-13 11:42:29.386: E/AndroidRuntime(894): at com.owentech.testswipeab.TabTwo$loadMoreListView.doInBackground(TabTwo.java:372) 
07-13 11:42:29.386: E/AndroidRuntime(894): at com.owentech.testswipeab.TabTwo$loadMoreListView.doInBackground(TabTwo.java:1) 
07-13 11:42:29.386: E/AndroidRuntime(894): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
07-13 11:42:29.386: E/AndroidRuntime(894): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
07-13 11:42:29.386: E/AndroidRuntime(894): ... 5 more 

------------------------------------------------------------------------ 

回答

1

不能從單獨的線程觸動你View對象,你知道, doInBackground()在單獨的線程中運行。所以對ListView的調用將導致拋出異常。將所有View-移動代碼移動到onPostExecute()。希望這可以幫助。

+0

謝謝大家..這alsoi想包括在我的代碼,因爲我ruuning在4.0 SDK靜態{ \t \t \t如果(android.os.Build.VERSION.SDK_INT> 14) \t \t \t { \t \t \t StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); \t \t \t StrictMode.setThreadPolicy(policy); \t \t \t} \t \t \t}這看起來更高os版本4.0 emultor。 – Kstar 2012-07-13 07:15:51

+0

@Kstar,不客氣! – Egor 2012-07-13 07:16:44

0

您正嘗試從後臺線程更改UI,這是不可能的。

  1. 你可以看到進度條,爲preExceute正在做的工作細
  2. 在doInBackground您要設置在主活動UI這是不對的適配器。 doInBackground用於後臺進程並請註釋該函數(@Override)
  3. 調試的某些內容: - 請參閱日誌中的錯誤消息「由於:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有創建視圖層次結構的原始線程可以觸摸的意見」 ..This行應實際上已經告訴你,你在做什麼錯
4

doInBackground()執行其他線程。在doInBackground()中,您嘗試更新listViewattlist,它是在UI線程中創建的。這是一個錯誤,您應該在onPostExecute()方法中更新listViewattlist