Android AsyncTask:每个viewpager片段的async-task

时间:2013-08-07 04:49:35

标签: android android-asynctask android-viewpager android-handler

我有viewpagerFragmentStatePagerAdapter。我在AsyncTask中使用了Fragment,因为Fragment中有计算过程。这些片段将从FragmentStatePagerAdapter加载。这是片段中的快照代码..

public void refreshView() {
        if(asynTask == null) {
            asynTask = new SetupTableTask();
        }
        asynTask.execute();
    }

    private class SetupTableTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            setupInitialValue();
            dashboradView.removeAllViews();
            setupTableRow();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(getActivity(), "Dashboard",
                    " Loading...", true, false);
        }
    }

问题在于,当我快速滑动4或5页时。我得到了这个例外。

08-07 10:48:36.366: E/AndroidRuntime(5702): FATAL EXCEPTION: AsyncTask #2
08-07 10:48:36.366: E/AndroidRuntime(5702): java.lang.RuntimeException: An error occured while executing doInBackground()
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at java.lang.Thread.run(Thread.java:856)
08-07 10:48:36.366: E/AndroidRuntime(5702): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.View.requestLayout(View.java:15129)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.View.requestLayout(View.java:15129)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.View.requestLayout(View.java:15129)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.View.requestLayout(View.java:15129)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.View.requestLayout(View.java:15129)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.View.requestLayout(View.java:15129)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.view.View.requestLayout(View.java:15129)
08-07 10:48:36.366: E/AndroidRuntime(5702):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)

我想知道我可以在ViewPager的片段中使用AsyncTask吗?有什么想法克服这个例外吗?我已经尝试了Handler。我得到了像CalledFromWrongThreadException一样的异常。

由于

1 个答案:

答案 0 :(得分:2)

您无法在AsyncTask doInBackground中绑定或删除任何类型的视图

  dashboradView.removeAllViews();

doInBackground中删除它 并放入onPostExecute,在这里你可以绑定你的观点