ericwlange / AndroidJSCore

AndroidJSCore allows Android developers to use JavaScript natively in their apps.
468 stars 78 forks source link

App crashes when trying to pass a Java callback #42

Closed kasuboski closed 7 years ago

kasuboski commented 7 years ago

I'm trying to pass a java object for the JS to use as a callback, but when called the app crashes with: Abort message: 'art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: JNI SetLongField called with pending exception java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object[] org.liquidplayer.webkit.javascriptcore.JSBaseArray.toArray(java.lang.Class)' on a null object reference'

Java call:

JSFunction func = jsContext.property("callbackTest").toFunction();
        NetworkCallback callback = new NetworkCallback(jsContext) {
            @Override
            public void onSuccess(Object... args) {
                if(null != args[0]) {
                    Log.d(TAG, args[0].toString());
                }
            }

            @Override
            public void onError(Object... args) {
                Log.e(TAG, args[0].toString());
            }
        };

        func.call(null, callback);

JS:

function callbackTest(callback) {
    console.log('Callback Test');//I mapped this to Android Log it works
    callback.onSuccess("Hurray");//this is where the exception happens
}

I have NetworkCallback extending JSObject and implementing an interface. It is declared as abstract. Is this possibly the issue?

If this isn't possible is there a preferred method to get values back to Java after an asynchronous event in Javascript?

kasuboski commented 7 years ago

After changing the argument of the callback methods to a String it seems to work. I'm guessing there just isn't support for Object...?

ericwlange commented 7 years ago

It doesn't really know what to do with Object. If you want a generic type, use JSValue.

On Monday, September 26, 2016, Josh Kasuboski notifications@github.com wrote:

After changing the argument of the callback methods to a String it seems to work. I'm guessing there just isn't support for Object...?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ericwlange/AndroidJSCore/issues/42#issuecomment-249628744, or mute the thread https://github.com/notifications/unsubscribe-auth/ABIuZNbaeuYH_iee0H1kLu3zSI-DV5sEks5qt_iqgaJpZM4KGtN3 .