当我尝试使用Android 4.4(Kit kat)在我的nexus 4上打开webview时,我会收到以下错误消息:
Calling View methods on another thread than the UI thread.;
java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:268)
自从我更新到Android 4.4我的Nexus 4。
只需检查4.4谷歌的迁移网页视图添加并更改了一些内容here
runOnUiThread(new Runnable() {
@Override
public void run() {
// Code for WebView goes here
}
});
// This code is BAD and will block the UI thread
webView.loadUrl("javascript:fn()");
while(result == null) {
Thread.sleep(100);
}
你的代码是什么样的?你可以试试
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Your code
}
});
如果runOnUiThread由于某种原因不可用,您还可以使用以下代码来绕过此错误(假设您想要访问View而不需要显示它):
Handler handler = new Handler(Looper.getMainLooper());
try
{
handler.post(
new Runnable()
{
@Override
public void run()
{
DesiredMethod(); // Where this method runs the code you're needing
}
}
);
} catch (Exception e)
{
e.printStackTrace();
}