問題是:html
A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same
我英語很差看不太懂,大體的瞭解了下緣由。java
以前個人代碼是這樣。android
1 @JavascriptInterface 2 public void needLogin(String username,String password) { 3 if (username.equals("abc")&&password.equals("123")){ 4 Toast.makeText(MainActivity.this,"登陸成功",Toast.LENGTH_SHORT).show(); 5 webView.loadUrl("file:///android_res/raw/test1.html"); 6 }else { 7 Toast.makeText(MainActivity.this,"登陸失敗",Toast.LENGTH_SHORT).show(); 8 } 9 }
運行就錯了,就是在webView從新loadUrl的時候出現了問題。web
而後我這樣改了下,就ok了,ide
1 @JavascriptInterface 2 public void needLogin(String username,String password) { 3 if (username.equals("abc")&&password.equals("123")){ 4 Toast.makeText(MainActivity.this,"登陸成功",Toast.LENGTH_SHORT).show(); 5 webView.post(new Runnable() { 6 @Override 7 public void run() { 8 webView.loadUrl("file:///android_res/raw/test1.html"); 9 } 10 }); 11 12 }else { 13 Toast.makeText(MainActivity.this,"登陸失敗",Toast.LENGTH_SHORT).show(); 14 } 15 }
我想你們確定猜到了什麼了。webView會checkThread,致使不行,由於在js調用後的Java回調線程並非主線程。post