Android4.1 JELLY_BEAN: All WebView methods must be called on the same thread [problem resolved], android4.1webview

Source: Internet
Author: User

Android4.1 JELLY_BEAN: All WebView methods must be called on the same thread [problem resolved], 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 (main, tid 1) {export f48a8} called on Looper (JavaBridge, tid 92104) {export 508d0}, FYI main loridge is Looper (main, tid 1) {rjf48a8 })



Today, a friend from the group came in and asked this question. I prompted him: the exception message is very clear. All webview methods must be in one thread than calls, the two methods call tid is obviously different.

Let's look at the code he wrote first:

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); // find WebViewwebView by ID. getSettings (). setJavaScriptEnabled (true); // allows JSwebView. loadUrl ("file: // android_asset/index.html"); // load the webView of the page. addJavascriptInterface (new Contact (), "contact"); // create a Contact object and send it to WebView as a JS object} class Contact {@ JavascriptInterfacepublic void showContacts () {/* handler. post (new Runnable () {@ Overridepublic void run () {String json = "[{name: \" \ ", amount: \" 12345 \ ", phone: \ "18600012345 \" },{ name: \ "Dawn \", amount: \ "54321 \", phone: \ "18600054321 \"}] "; webView. loadUrl ("javascript: show ('" + json + "')"); // call the JS method // pass the js data to the html page }}); */String json = "[{name: \" \ ", amount: \" 12345 \ ", phone: \" 18600012345 \ "}, {name: \ "Dawn \", amount: \ "54321 \", phone: \ "18600054321 \"}] "; webView. loadUrl ("javascript: show ('" + json + "')"); // call the JS method // call the js data, pass to html page} @ 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 )));}}}

The above code is actually not a problem in versions earlier than android4.4. I have written this before, but Android 4.1, API 17, and JELLY_BEAN started, android has made some changes to the interaction between js Code Execution and native code in webview. I have not studied any specific changes, but just provided a solution based on the exception information;

For the above Code, you only need to put the call () and showContacts () methods called by js in the same handler. post thread for execution.


In addition,Only public methods identified by JavascriptInterface annotations can be accessed by JS Code.You must remember this.

@JavascriptInterfacepublic void showContacts() {

If the method you provide for js calls in Versions later than Android is not annotated, this method cannot be accessed by javascript in webview. Many people also ask this question.


Check the final execution result:



The index.html code under the assetsdirectory:

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 



Source code download: (upload as needed)



  • Welcome to CSDN technology group: 221057495


Error reported when running The Android project on The simulator: The application may be doing too much work on its main thread

The simulator is crazy .. I tried to pass the test. The simulator is not restarted.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.