11-06 18:29:15.582: W/WebView(27807): 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) {425f48a8} called on Looper (JavaBridge, tid 92104) {426508d0}, FYI main Looper is Looper (main, tid 1) {425f48a8})


android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[问题已解决]

今天群里的一个朋友进来问到此问题。我提示他:异常信息提示非常明白,全部的webview的方法比调用必须在一个线程,2个方法调用tid明显不同嘛。

我们先看他写得代码:

package com.webview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

public class MainActivity extends Activity {
	private Handler handler= new Handler();
	private WebView webView;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		webView = (WebView) findViewById(R.id.webView);				// 依据ID找到WebView
		webView.getSettings().setJavaScriptEnabled(true);			// 同意JS
		webView.loadUrl("file:///android_asset/index.html");		// 载入页面
	    webView.addJavascriptInterface(new Contact(), "contact");	// 创建Contact对象, 传给WebView, 作为JS对象
		
}
	
	class Contact {
		@JavascriptInterface
		public void showContacts() {
/*			handler.post(new Runnable(){
				@Override
				public void run(){
					String json = "[{name:\"王小二\", amount:\"12345\", phone:\"18600012345\"}, {name:\"黎明\", amount:\"54321\", phone:\"18600054321\"}]";
					webView.loadUrl("javascript:show('" + json + "')");		// 调用JS方法//   把js数据,传递给html页面
				}
			});*/
			
			String json = "[{name:\"王小二\", amount:\"12345\", phone:\"18600012345\"}, {name:\"黎明\", amount:\"54321\", phone:\"18600054321\"}]";
			webView.loadUrl("javascript:show('" + json + "')");		// 调用JS方法//   把js数据,传递给html页面

		}
		@JavascriptInterface
		public void call(final String phone) {
			
/*			handler.post(new Runnable() {
				@Override
				public void run(){
					startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel://" + phone)));
				}
			});*/
			
			startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel://" + phone)));
		}
	}
	
 
}

上面代码事实上在android4.4下面版本号也不会出什么问题,曾经我也这么写过。可是 Android 4.1,API 17,也就是JELLY_BEAN 開始,android就针对webview中运行js代码和原生代码之间交互做了一些修改,详细修改什么我也没有去研究。仅仅是把按照异常信息给出解决方法而已。

上述代码。仅仅要把js调用的方法  call()和 showContacts()都放在同一个handler.post线程运行就没有问题了。


此外。仅仅有被JavascriptInterface 注解标识的公有方法能够被JS代码訪问,大家一定记住这一点

@JavascriptInterface
		public void showContacts() {

假设在android4.1以上版本号。你提供给js调用的方法,没有标示注解。这种方法是无法被webview里的javascript訪问到的,非常多人也问到这个问题了。


看看终于运行效果:

android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[问题已解决]


assets文件夹下的index.html代码:




源码下载:(有人须要我就上传)



  • 欢迎增加CSDN技术群:221057495 交流

相关文章:

  • 2022-12-23
  • 2022-01-15
  • 2022-02-01
  • 2021-06-28
  • 2022-12-23
  • 2021-05-29
  • 2022-12-23
  • 2021-04-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案