单独线程更新 UI 中长时间运行的进程
我注意到 dot NET 也有类似的问题,但我的问题是针对 Android 的,所以也许解决方案看起来不同。
通过单击按钮激活该过程。该进程作为 UI 线程的一部分运行,最后更新 UI。我添加了进度对话框以更加用户友好,因此我实例化了一个运行该进程的线程,最后它更新 UI 并关闭进度对话框。不幸的是,UI 更新失败,但出现以下异常:
07-19 21:14:04.602: ERROR/Atjeews(283): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
如果我尝试阻止 UI 线程并在长时间进程完成更新 UI 后释放它,则不会显示进度对话框。我应该尝试在单独的线程中显示进度对话框,还是有另一个更简单的解决方案?
I noticed a similar problem for dot NET, but my problem is for Android, so perhaps solution looks different.
The process is activated by clicking button. The process was running as part of UI thread and at the end it did updating UI. I have added progress dialog to be more user friendly, so I instantiate a thread running the process and at the end it updates UI and dismisses progress dialog. Unfortunately UI update is failing with the exception below:
07-19 21:14:04.602: ERROR/Atjeews(283): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
If I try to block UI thread and release it after long process finishes to update UI, the progress dialog doesn't get shown. Should I try to show progress dialog in a separate thread instead, or there is another simpler solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请检查http://developer.android.com/resources/articles/painless-threading .html
please check http://developer.android.com/resources/articles/painless-threading.html
摘自 Android 开发人员进程和线程页面:
您可以实例化一个匿名 Runnable 作为参数,例如
post(new Runnable(){doWhatever();})
,它将执行您想要的任何操作而是在 UI 线程上。Taken from the Android Developers Processes and Threads page:
You can just instantiate an anonymous Runnable as the argument, like
post(new Runnable(){doWhatever();})
, that will do whatever you want on the UI thread instead.