无法让 ProgressDialog 在线程中显示
在尝试让它发挥作用的过程中,我见过各种各样的失败。我有一个通过活动启动的线程。线程需要创建/显示进度对话框并关闭它们。
当我尝试直接显示 ProgressDialog 时,出现错误,表明我的 Looper 未准备好。我用 Looper 查找了一下,它是一个实现的。但是,我必须调用 Looper.loop 才能显示进度对话框。它出现后,应用程序冻结在该点上,永远不会继续执行 Looper.loop 调用。
我无法让它工作,所以寻找一种使用 HandlerThread 和 Handler 的全新方法。我创建一个 HandlerThread 并启动它。我从线程中获取循环器并用它创建一个处理程序。我的 ProgressDialog 或 Toasts 根本不会显示。
有没有更简单的方法来做到这一点?
Well I've seen a wide variety of failures while trying to get this to work. I have a thread that is started via an Activity. The thread needs to create/display progress dialogs and dismiss them.
When I tried to directly display the ProgressDialog I got an error that my Looper wasn't prepared. I looked up with a Looper was an implemented it. However, I had to call Looper.loop for the progress dialog to show up. After it showed up the application froze on that point never to continue past the Looper.loop call.
I couldn't get it to work so looked for a whole new way using a HandlerThread and a Handler. I create a HandlerThread and start it. I get the looper from the thread and create a Handler with it. My ProgressDialog or Toasts won't show up at all.
Is there an easier way to go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以设置一个
,以便在启动线程时启动进度条,并在线程完成后停止进度条。
这将有助于调用
progressbar
来启动和停止。这将是解决方案之一。U can have an
so that when u start the thread start the
progressbar
and after thread is completed u can stop theprogressbar
.This will help to call the
progressbar
to start and stop.. This will be one of the solution..不确定 ProgressDialog,但据我所知,Android 中所有与 UI 相关的内容都需要在 UI Thread 中更新。实际上有一个简单的帮助器类用于实现异步任务:http://developer.android。 com/reference/android/os/AsyncTask.html
或者,您可以创建一个处理程序(位于 UI 线程上)并使用它创建对话框:
Not sure about ProgressDialog, but all UI related stuff in Android, as far as I know, required to be updated in UI Thread. There's actually an easy helper class for implementing async task: http://developer.android.com/reference/android/os/AsyncTask.html
Alternatively, you can create a Handler (which would be on UI Thread) and create the dialog using that:
最后一个答案是错误的......
它应该是:
setProgressBarIndependentVisibility(Boolean.TRUE | Boolean.FALSE);
The last answer is wrong....
it should be:
setProgressBarIndeterminateVisibility(Boolean.TRUE | Boolean.FALSE);