更改 ProgressBar 的可见性会导致强制关闭
我有一个包含 WebView 的布局,以及位于其顶部中心的 ProgressBar。进度条需要以编程方式显示和隐藏(当网页内容加载时)。但是,使用 loading.setVisibility(View.VISIBLE);
将 ProgressBar 设置为可见会导致强制关闭。如果进度条默认可见,则可以正常工作。如果需要,我将粘贴所有适当的代码,但我怀疑我做的事情根本上是错误的。
(应该有强制关闭标签,但它和强制关闭都不存在,我无法创建它。)
好的,代码。 setVisibility 很简单:
public void nowLoading() {
loading.setVisibility(View.VISIBLE);
}
通过 javascript 调用 nowLoading...,并在 WebView 上使用 addJavascriptInterface。啊...我想 WevView 是在不同的线程中。我该如何解决这个问题?
I have a layout containing a WebView, and a ProgressBar centred on top of it. The progress bar needs to be shown and hid programatticaly (as web content loads). However, setting the ProgressBar to be visible using loading.setVisibility(View.VISIBLE);
, causes a force close. If the ProgressBar is visible by default it works fine. I will paste all appropriate code if needed, but I suspect I'm doing something fundamentally and simply wrong.
(should have force-close tag but neither it nor forceclose exists and I can't create it.)
Okay, code. The setVisibility is simply:
public void nowLoading() {
loading.setVisibility(View.VISIBLE);
}
nowLoading is called... via javascript, with addJavascriptInterface on the WebView. Ahh... I imagine the WevView is in a different Thread. How do I solve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能正在从错误的线程更改可见性。您是否在使用 Thread.start() 启动的新线程中进行可见性更改?
编辑:使用
Handler
(参见 http://developer .android.com/reference/android/os/Handler.html)。向处理程序发送消息,然后从处理程序执行可见性更改。或者使用View.post
(参见 http://developer.android.com/reference/android/view/View.html#post%28java.lang.Runnable%29)。You are probably changing the visibility from the wrong thread. Are you doing the visibility change in a new Thread you started with
Thread.start()
?EDIT: use a
Handler
(see http://developer.android.com/reference/android/os/Handler.html). Send a Message to the Handler and then do the visibility change from the Handler. Or useView.post
(see http://developer.android.com/reference/android/view/View.html#post%28java.lang.Runnable%29).试试这个:
try this :