相同的线程错误的WebView方法

我有一个Android程序(在web视图中的Java +的HTML)。 我可以从JavaScript调用Java代码。 但是另一种方式停止工作(在eclipse中更新之后)。

所以这就是我想要做的

  • 做一个webview(工作)
  • 在JavaScript中调用AndroidFunction.test(); (工作)
  • java test()函数调用webView.loadUrl(“javascript:helloBack()”); (!不工作了)

我试图让它在MainActivity的WebView中工作,但它没有工作。

MainActivity.java

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final WebView webView = (WebView)findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient()); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); javascr = new Javascript(this, webView); webView.addJavascriptInterface(javascr, "AndroidFunction"); webView.loadUrl("file:///android_asset/www/index.html"); .... } 

Javascript.java

 public class Javascript { Context cont; WebView webView; Javascript(Context c, WebView w) { cont = c; webView = w; } // function called in the javascript by AndroidFunction.test(); public void test() { // Breaking point!!! webView.loadUrl("javascript:helloBack()"); } 

错误:

 03-24 11:47:50.103: W/WebView(21026): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27) 03-24 11:47:50.103: W/WebView(21026): java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper{41ab68f8} called on Looper{41bb70a8}, FYI main Looper is Looper{41ab68f8}) 03-24 11:47:50.103: W/WebView(21026): at android.webkit.WebView.checkThread(WebView.java:2063) 03-24 11:47:50.103: W/WebView(21026): at android.webkit.WebView.loadUrl(WebView.java:794) 03-24 11:47:50.103: W/WebView(21026): at com.example.hellobt.Javascript.test(Javascript.java:24) 03-24 11:47:50.103: W/WebView(21026): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) 03-24 11:47:50.103: W/WebView(21026): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27) 03-24 11:47:50.103: W/WebView(21026): at android.os.Handler.dispatchMessage(Handler.java:102) 03-24 11:47:50.103: W/WebView(21026): at android.os.Looper.loop(Looper.java:137) 03-24 11:47:50.103: W/WebView(21026): at android.os.HandlerThread.run(HandlerThread.java:61) 

感谢你的回答。 我编辑我的JavaScript文件中的function是这样的:

 private void test(final String s) { webView.post(new Runnable() { public void run() { webView.loadUrl("javascript:" + s + ";"); } }); System.out.println("javscript done.."); } 

JavaScript方法在后台(即非UI)线程上执行。 您需要在UI线程上调用所有与Android视图相关的方法。 您可以通过以下方式实现您的需求:

 mWebView.post(new Runnable() { @Override public void run() { mWebView.loadUrl(...). } }); 

这将发布任务在UI线程上运行。

在我的情况下,WebView中没有显示任何内容,所以我更喜欢另一种方式:

 runOnUiThread(new Runnable() { @Override public void run() { final WebView webView = (WebView) findViewById(R.id.map); webView.loadDataWithBaseURL(...); } }); 

这可以通过使用post方法来完成。 请通过下面的代码。

  m_targetView.post(new Runnable() { @Override public void run() { m_targetView.loadUrl("....."); } });