SoFunction
Updated on 2025-03-10

Solution to the error of webView calling JS in Android

question

A webView error occurred when calling JS.

Copy the codeThe code is as follows:

    class TestJS {
        ......
        public TestJS(){
        }
       
        public void save(String data){           
            ("javascript: alert(" + data +")");
        }
        ......
    }

Copy the codeThe code is as follows:

    W/WebView(2088): : A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {b3dbcb18} called on Looper (JavaBridge, tid 120) {b44a1af8}, FYI main Looper is Looper (main, tid 1) {b3dbcb18})
    W/WebView(2088):     at (:2063)
    W/WebView(2088):     at (:794)
    W/WebView(2088):     at (:180)
    W/WebView(2088):     at $(:193)
    W/WebView(2088):     at (Native Method)
    W/WebView(2088):     at (:27)
    W/WebView(2088):     at (:102)
    W/WebView(2088):     at (:136)
    W/WebView(2088):     at (:61)

solve

Modify the save method to:

Copy the codeThe code is as follows:

    public void save(String data){           
        (new Runnable() {
            @Override
            public void run() {
                ("javascript: alert(" + data +")");
            }
        });
    }

The above is the solution. Isn’t it very simple? I hope you guys like it.