Alex Rivera | Logout

Error calling method on NPObject! in Android 2.2

Asked 2010-07-27T13:44:23.373
11

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.

Edit
Report

2 Answers

11

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! :)

answered 2012-05-18T16:30:08.813
5

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.

answered 2012-01-03T20:33:03.973

Your Answer