gpt4 book ai didi

安卓异步任务 : async-task for each viewpager fragment

转载 作者:行者123 更新时间:2023-11-29 00:27:50 25 4
gpt4 key购买 nike

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

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 页时。我得到了这个异常(exception)。

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 的 fragment 中使用 AsyncTask 吗?有什么想法可以克服这个异常(exception)吗?我已经尝试过 Handler。我得到了与 CalledFromWrongThreadException 相同的异常。

谢谢

最佳答案

你不能在 AsyncTask doInBackground 中绑定(bind)或删除任何类型的 View

  dashboradView.removeAllViews();

将其从 doInBackground 中移除并放入 onPostExecute 在这里你可以绑定(bind)你的 View

关于安卓异步任务 : async-task for each viewpager fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18094900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com