首页 > 代码库 > [原][Android]All WebView methods must be called on the same thread.
[原][Android]All WebView methods must be called on the same thread.
问题
webView调用JS出错。
class TestJS {
......
public TestJS(){
}
public void save(String data){
webView.loadUrl("javascript: alert(" + data +")");
}
......
}
W/WebView(2088): java.lang.Throwable: A WebView method was called on thread ‘JavaBridge‘. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {b3dbcb18} called on Looper (JavaBridge, tid 120) {b44a1af8}, FYI main Looper is Looper (main, tid 1) {b3dbcb18})
W/WebView(2088): at android.webkit.WebView.checkThread(WebView.java:2063)
W/WebView(2088): at android.webkit.WebView.loadUrl(WebView.java:794)
W/WebView(2088): at com.ue.oa.activity.XFormActivity.alert(XFormActivity.java:180)
W/WebView(2088): at com.ue.oa.activity.XFormActivity$FormActions.save(XFormActivity.java:193)
W/WebView(2088): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/WebView(2088): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
W/WebView(2088): at android.os.Handler.dispatchMessage(Handler.java:102)
W/WebView(2088): at android.os.Looper.loop(Looper.java:136)
W/WebView(2088): at android.os.HandlerThread.run(HandlerThread.java:61)
W/WebView(2088): at android.webkit.WebView.checkThread(WebView.java:2063)
W/WebView(2088): at android.webkit.WebView.loadUrl(WebView.java:794)
W/WebView(2088): at com.ue.oa.activity.XFormActivity.alert(XFormActivity.java:180)
W/WebView(2088): at com.ue.oa.activity.XFormActivity$FormActions.save(XFormActivity.java:193)
W/WebView(2088): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/WebView(2088): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
W/WebView(2088): at android.os.Handler.dispatchMessage(Handler.java:102)
W/WebView(2088): at android.os.Looper.loop(Looper.java:136)
W/WebView(2088): at android.os.HandlerThread.run(HandlerThread.java:61)
解决
将save方法修改为:
public void save(String data){
webView.post(new Runnable() {
@Override
public void run() {
webView.loadUrl("javascript: alert(" + data +")");
}
});
}
webView.post(new Runnable() {
@Override
public void run() {
webView.loadUrl("javascript: alert(" + data +")");
}
});
}
【Reference】
[1] 《android webview.loadUrl won‘t load another webpage》 : http://stackoverflow.com/questions/21955593/android-webview-loadurl-wont-load-another-webpage
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。