开发者

Error calling method on NPObject! in Android 2.2

I'm using addJavascriptInterface within my Android application to allow JavaScript to invoke functions I've created in my native Java application.

This worked well in Android 2.1, however in Android 2.2 I 开发者_开发百科receive the error message "Error calling method on NPObject!"

When I instrument the method call the internals of the native method are getting called, however the exception is being throw in JavaScript.


I was getting this exact error:

Uncaught Error: Error calling method on NPObject!

Turns out I was attempting to invoke a JavascriptInterface function from a webview like so:

AndroidJS.populateField(field);

and on the Java side, the function didn't accept a parameter:

public void populateField() {}

Simply allowing the Java function to accept a parameter solved this error for me.

E.g., public void populateField(String field) {}

This may not be, and probably is not, the only reason this error could be thrown. This is simply how I resolved my specific scenario. Hope this helps! :)


OK, I have same problem as well, just in today.

What I did is putting code in UI Thread, like code below :

/**
 * 給網頁Javascript呼叫的method
 * Method for Javascript in HTML
 * @param java.lang.String - Playlist ID
 */
public int callListByPID(final String pId)
{
  Log.i(Constants.TAG, "PAD Playlist ID from HTML: "+pId);

  runOnUiThread(new Runnable()
  {
    public void run()
    {
      // Put your code here...
    }
  });

  return 1;
}

This solved my problem, and hope it can help some body... :-)


In my experience this problem is caused by Javascript interfaces bringing back objects that Javascript doesn't automatically identify.

In Android this is caused by wrappers like Boolean or Long in comparison to their native versions boolean and long.

//This will fail
public Long getmyLongVal() {
    return 123456789;
}

//This will work
public long getMyNativeLongVal() {
    return 123456789;
}

So remove your wrapper classes to any methods being used by Javascript if you want to avoid NPObject errors.


Here's a twist I found on this problem that could be useful for some of the folks running into this problem (and it likely explains intermittent failures that seem to defy explanation)...

If any exceptions are thrown (and not caught) in the return handler code prior to allowing the javascript interface callback to return clean, it will propagate back as a failed call and you will also get this error - and it would have nothing to do with missing functions or parameters.

The easiest way to find this case (whether or not you use this in your final implementation) is to push whatever handler code you have back onto the UI thread (the callback will not be on the UI thread) - this will allow the callback to return clean and any subsequent exceptions that occur will propagate properly up until you catch them or until the app crashes. Either way you will see exactly what is really happening. Otherwise the uncaught exception passes back to javascript where it will not be handled or reported in any way (unless you specifically built error trapping code into the JS you were executing).

Good Luck All.

bh


I had the same problem with Javascript-to-Java interface (WebView.addJavascriptInterface).

In Android 2.1 everything worked just fine but in Android 2.2 Javascript failed to call methods from this interface. It returned an error: Uncaught Error: Error calling method on NPObject!

It seems that on Android 2.2, the WebView has problem with Boolean data type returned from interface functions.

Changing:

public Boolean test_func() { return true; }

... to:

public int test_func() { return 1; }

... solved the problem.


This I believe is no longer supported anymore ( Always game NPObject error ) . Please refer to the answer in this thread Visit open an activity from a CordovaPlugin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜