gpt4 book ai didi

java - 移除 ViewGroup 中的 View

转载 作者:行者123 更新时间:2023-11-29 05:49:38 24 4
gpt4 key购买 nike

我在使用 ViewGroup 和自动生成的 ViewPager 时遇到了一些问题。我想添加一个 View 并删除一个 View ,但每次尝试时我都会收到错误消息:

    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 的公共(public) ViewGroup。在所有数据处理结束时,我尝试 vGroup.removeView(vGroup.findViewById(1));vGroup.addView(modeList); 但我得到上面的错误.

我怎样才能到达原始线程,我可以在其中向此ViewGroup 添加和删除 View ?

最佳答案

ViewRootImpl$CalledFromWrongThreadException: 只有创建 View 层次结构的原始线程才能触及它的 View 。 意味着您正在尝试从后台线程(即非 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) {
}
}

Documentation of AsyncTask .

关于java - 移除 ViewGroup 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424923/

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