Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 correct string within my Android application. The problem: Sometimes I get an Uncaught Error: Error calling method on NPObject.

Does anybody know why?

The Interface in Java:

public class JSInterfaceGame extends JSInterface {

@JavascriptInterface
public void setShareText(String share){
    shareText = share;
    if(mJSInterfaceListener != null)
        mJSInterfaceListener.onParametersChanged(SHARE_TEXT);
}

The initialization in the onCreateView-Method within the Fragment:

online = (WebView) rootView.findViewById(R.id.online);
online.setWebViewClient(new WISWebviewClient() {
  @Override
  public void onStatusChanged(final WebView view, int progress, long duration) {
    //unrelated
  }
});

WebSettings ws = online.getSettings();
ws.setJavaScriptEnabled(true);
ws.setUserAgentString(USER_AGENT);
ws.setCacheMode(WebSettings.LOAD_DEFAULT);
ws.setRenderPriority(WebSettings.RenderPriority.HIGH);

SharedPreferences settings = getActivity().getSharedPreferences(GameActivity.PREFERENCES, Context.MODE_PRIVATE);

mJSInterface = new JSInterfaceGame();
mJSInterface.setJSInterfaceListener(this); // Defined elsewhere in this class.
mJSInterface.setPlayerName(settings.getString(GameActivity.PREFS_PlAYERNAME, null));
online.addJavascriptInterface(mJSInterface, "JSInterface");
online.loadUrl("http://myurl.something");

Call in Javascript:

function makeShareText() {
  var text = "Some text";
  console.log(typeof text); // Always a string.
  JSInterface.setShareText(text);
}
like image 419
janwo Avatar asked May 26 '13 10:05

janwo


3 Answers

It happens when you try, using method called from javascript interface, to interact with UI. To solved it in this way:

class mJSInterface() {  public void myFunction() {     runOnUiThread(new Runnable() {              public void run() {                 //Code that interact with UI             }         });      }  } 
like image 176
Nico.S Avatar answered Sep 20 '22 16:09

Nico.S


To highlight the comment from @Leog

The same error occurs if you call the native javascript function with wrong parameters

This was the source of my error

like image 27
cloakedninjas Avatar answered Sep 22 '22 16:09

cloakedninjas


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.

like image 30
dobridog Avatar answered Sep 21 '22 16:09

dobridog