我使用ViewGroup和自动生成的ViewPager遇到了一些问题。 我想添加一个视图并删除一个视图,但每次我尝试它时都会收到错误:
01-20 14:05:40.028: E/AndroidRuntime(3615): FATAL EXCEPTION: AsyncTask #1
01-20 14:05:40.028: E/AndroidRuntime(3615): java.lang.RuntimeException: An error occured while executing doInBackground()
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-20 14:05:40.028: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-20 14:05:40.028: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-20 14:05:40.028: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-20 14:05:40.028: E/AndroidRuntime(3615): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-20 14:05:40.028: E/AndroidRuntime(3615): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-20 14:05:40.028: E/AndroidRuntime(3615): at java.lang.Thread.run(Thread.java:856)
01-20 14:05:40.028: E/AndroidRuntime(3615): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.view.View.requestLayout(View.java:15468)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.view.View.requestLayout(View.java:15468)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.view.View.requestLayout(View.java:15468)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.view.View.requestLayout(View.java:15468)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.view.ViewGroup.removeView(ViewGroup.java:3524)
01-20 14:05:40.028: E/AndroidRuntime(3615): at de.tecfriends.vbtsplash2013.MainActivity$DummySectionFragment.Teilnehmer(MainActivity.java:219)
01-20 14:05:40.028: E/AndroidRuntime(3615): at de.tecfriends.vbtsplash2013.MainActivity$DummySectionFragment$1.onTaskCompleted(MainActivity.java:148)
01-20 14:05:40.028: E/AndroidRuntime(3615): at de.tecfriends.vbtsplash2013.BackgroundDownload.doInBackground(BackgroundDownload.java:38)
01-20 14:05:40.028: E/AndroidRuntime(3615): at de.tecfriends.vbtsplash2013.BackgroundDownload.doInBackground(BackgroundDownload.java:1)
01-20 14:05:40.028: E/AndroidRuntime(3615): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-20 14:05:40.028: E/AndroidRuntime(3615): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-20 14:05:40.028: E/AndroidRuntime(3615): ... 4 more
在班级onCreateView
中,我设置了一个名为vGroup
的公共ViewGroup。
在所有数据处理结束时,我尝试vGroup.removeView(vGroup.findViewById(1));
和vGroup.addView(modeList);
但我收到上述错误。
如何才能到original Thread
我可以添加和删除视图ViewGroup
?
答案 0 :(得分:2)
ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
表示您正在尝试从后台线程访问UI元素,后台线程是非UI线程。
vGroup.removeView()
或onPreExecute()
调用 onPostExecute()
,因为这两个函数在UI线程上运行。
private class MyAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
.... // runs on background / non-UI thread
}
@Override
protected void onPostExecute(String result) {
... // runs on UI thread
}
@Override
protected void onPreExecute() {
... // runs on UI thread
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
答案 1 :(得分:1)
您正在从后台线程调用vGroup.removeView()
。要更改UI,您应该/必须始终使用UI Thread
。
编辑:在Handler
上创建UIThread
并使用mHandler.post(new Runnable());
答案 2 :(得分:0)
在使用vGroup.removeView()
旁边,您需要致电vGroup.refreshDrawableState()