Sometimes throws Uncaught Error: Error calling method on NPObject on Android

后端 未结 4 712
栀梦
栀梦 2020-12-24 07:35

I am having problems with the Webview in Android and it\'s JavascriptInterfaces.

I am passing a string to the JavascriptInterface. When debugging it, I receive the c

4条回答
  •  星月不相逢
    2020-12-24 07:47

    Another reason can be a RuntimeException on a WebViewCoreThread. Any exception occurred after receiving @JavascriptInterface call will be logged as NPObject error if still running on a WebView thread. Overall insufficient trace message with little clue about the problem.

    Correct your issue with handling javascript interface call on a suitable thread.

    Example A. (NPObject error):

    @JavascriptInterface
    public void jsCall() {
        Log.v(TAG, "Prepared NullPointerException on "+Thread.currentThread());
        String s = null;
        s.length();  // This will cause NPObject error
    }
    

    Example B. (NullPointerException):

    @JavascriptInterface
    public void jsCall() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.v(TAG, "Prepared NullPointerException on " + Thread.currentThread());
                String s = null;
                s.length();  // This will throw NullPointerException
            }
        }).start();
    }
    

    Take this as an addition to @Nico.S's answer.

提交回复
热议问题