如何同时加载复杂视图

发布于 2024-09-13 14:12:25 字数 367 浏览 3 评论 0原文

我有一个创建自定义视图的 Android 活动,这是一个昂贵的过程。我尝试使用处理程序,但当我这样做时,显示的进度对话框每 5 秒旋转一次。所以我尝试使用 AsyncTask。现在,progressDialog 旋转,但随后崩溃,因为不允许加载 UI?我怎样才能做到这一点而不收到 ANR?

08-10 19:28:21.313: ERROR/AndroidRuntime(1428): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I have an Android activity that creates custom views, and it is an expensive process. I tried to use a Handler, but when I do, the progress dialog that shows, spins like once every 5 seconds. So I tried to use an AsyncTask. Now the progressDialog spins, but then it crashes, because it isn't allowed to load the UI? How can I do this without getting ANR?

08-10 19:28:21.313: ERROR/AndroidRuntime(1428): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫瑟鸿黎 2024-09-20 14:12:25

我有一个创建自定义视图的 Android 活动,这是一个昂贵的过程。

使用 Traceview 或其他工具来提高性能。创建自定义视图本质上并不昂贵。

所以我尝试使用 AsyncTask。现在
进度对话框旋转,但随后它
崩溃,因为不允许
加载用户界面?

您通常不能在后台线程中执行与 UI 相关的工作。如果小心的话,您可能能够构建小部件,但您绝对不能将它们附加到屏幕(即,它们不能被引用为您的内容视图的子级)。

如何在不收到 ANR 的情况下执行此操作?

我将专注于解决您的性能问题,使用 Traceview 或其他工具来找出您的时间花在哪里的根源。

I have an Android activity that creates custom views, and it is an expensive process.

Use Traceview or other tools to improve your performance. Custom views are not intrinsically expensive to create.

So I tried to use an AsyncTask. Now
the progressDialog spins, but then it
crashes, because it isn't allowed to
load the UI?

You generally cannot do UI-related work in a background thread. With some care, you may be able to construct widgets, but you definitely cannot attach them to the screen (i.e., they can't be referenced as children of whatever is your content view).

How can I do this without getting ANR?

I would focus on fixing your performance problem, using Traceview or other tools to get at the root of where your time is being spent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文