android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[問題已解決],android4.1webview

來源:互聯網
上載者:User

android4.1 JELLY_BEAN:All WebView methods must be called on the same thread[問題已解決],android4.1webview

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})



今天群裡的一個朋友進來問到此問題,我提示他:異常資訊提示很明確,所有的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")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);webView = (WebView) findViewById(R.id.webView);// 根據ID找到WebViewwebView.getSettings().setJavaScriptEnabled(true);// 允許JSwebView.loadUrl("file:///android_asset/index.html");// 載入頁面    webView.addJavascriptInterface(new Contact(), "contact");// 建立Contact對象, 傳給WebView, 作為JS對象}class Contact {@JavascriptInterfacepublic void showContacts() {/*handler.post(new Runnable(){@Overridepublic 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頁面}@JavascriptInterfacepublic void call(final String phone) {/*handler.post(new Runnable() {@Overridepublic 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代碼訪問,大家一定記住這一點

@JavascriptInterfacepublic void showContacts() {

如果在android4.1以上版本,你提供給js調用的方法,沒有標示註解,這個方法是無法被webview裡的javascript訪問到的,很多人也問到這個問題了。


看看最終執行效果:



assets目錄下的index.html代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript">function show(jsondata) {var jsonobjs = eval(jsondata); // 產生json數組var table = document.getElementById("personTable");for ( var y = 0; y < jsonobjs.length; y++) { // 遍曆json數組var tr = table.insertRow(table.rows.length); // 每個對象建立1個TRvar td1 = tr.insertCell(0);// 建立3個TDvar td2 = tr.insertCell(1);var td3 = tr.insertCell(2);td1.innerHTML = jsonobjs[y].name; // 設定TD的內容為json對象的name屬性td2.innerHTML = jsonobjs[y].amount;td3.innerHTML = "<a href='javascript:contact.call(\"" + jsonobjs[y].phone + "\")'>" + jsonobjs[y].phone + "</a>"; // 點擊超連結時執行Java方法}}</script></head><body onLoad="javascript:contact.showContacts()"><table width="100%" id="personTable"><tr><td width="30%">姓名</td><td width="30%" align="center">存款</td><td align="center">電話</td></tr></table></body></html>



原始碼下載:(有人需要我就上傳)



  • 歡迎加入CSDN技術群:221057495 交流


Android項目在類比機上運行報錯:The application may be doing too much work on its main thread

模擬器瘋了吧。。試過關了模擬器重啟沒有
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.