如何同时加载复杂视图
我有一个创建自定义视图的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Traceview 或其他工具来提高性能。创建自定义视图本质上并不昂贵。
您通常不能在后台线程中执行与 UI 相关的工作。如果小心的话,您可能能够构建小部件,但您绝对不能将它们附加到屏幕(即,它们不能被引用为您的内容视图的子级)。
我将专注于解决您的性能问题,使用 Traceview 或其他工具来找出您的时间花在哪里的根源。
Use Traceview or other tools to improve your performance. Custom views are not intrinsically expensive to create.
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).
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.