1
public class MainActivity extends Activity {
  private static WebView mWebView;  
  etc...

  // JS -> ANDROID
  public class WebAppInterface {
    Context mContext;

    WebAppInterface(Context c) {
      mContext = c;
    }

    @JavascriptInterface
    public void fn1() {
      mWebView.loadUrl("javascript:jsfn('testing123');");
    }
  }
}

I getting this error:

W/WebView: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Wei Keat
  • 676
  • 2
  • 9
  • 12

1 Answers1

3

Callbacks from JavaScript through JavaScript interface object executes on a background thread where you can't directly update your UI thread. In order to post update in your UI thread you need to do the following.

@JavascriptInterface
public void fn1() {
        mWebView.post(new Runnable() {
            public void run() {
                mWebView.loadUrl("javascript:" + s + ";");
            }
        });
}
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25