0

Ok guys. What I'm trying to do: I've got some backend code written in javascript (the project was originally supposed to be a web-app) and I've added a javascript interface to my android web view, and when I press a button in my ui, the javascript code does some stuff, then calls a function in my javascript interface when it is done... The code from my javascript interface:

public void onReady() {
    backend.loadUrl("javascript:dokus.backend.onLoginStatusChange(Bridge.onLoginStatusChange);");
    backend.loadUrl("javascript:dokus.backend.onProjectsChange(Bridge.onProjectsChange);");
    backend.loadUrl("javascript:dokus.backend.onLatestEntriesChange(Bridge.onLatestEntriesChange);");
}

public void onLoginStatusChange (boolean loggedIn, String error) {
    Toast.makeText(mainActivity, "LoginStatusChange", Toast.LENGTH_SHORT).show();
}
...

I've already verified that the onReady function is called, so that's not the problem. The javascript function which calls onLoginStatusChange is this following:

function _login_callback(loggedIn, msg) {
    if (loggedIn) {
        _get_projects();
        _get_latest_entries();
    }

    console.log(_login_status_changed_callback);

    if (_login_status_changed_callback !== undefined)
        _login_status_changed_callback (loggedIn, msg);
}

The error i get is this: "Line: 180, type error". That's all i get from my WebChromeView function, that prints everything from console.log() to a toast message.

the console.log(_login_status_changed_callback) prints out the following:

function onLoginStatusChange () {
   [native code]
}

I've verified that the correct arguments are called to the function (a bool and a string), so that shouldn't be the error either... I'm at a loss, what do i do?

setup code for the web view:

private void setupBackend () {
    final DokusActivity that = this;

    backend = new WebView(this);
    BackendBridge.getInstance().initInstance(this, backend);

    backend.getSettings().setJavaScriptEnabled(true);
    backend.getSettings().setDomStorageEnabled(true);
    backend.addJavascriptInterface(BackendBridge.getInstance(), "Bridge");

    backend.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(that, "Oh no! " + description, Toast.LENGTH_SHORT).show();
        }
    });

    backend.setWebChromeClient(new WebChromeClient() {
        public boolean onConsoleMessage (ConsoleMessage consoleMessage) {
            Toast.makeText(that, "Oh no! " + consoleMessage.lineNumber() + ": " + consoleMessage.message(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    backend.loadUrl("file:///android_asset/index.html");
}
5
  • To see if I'm following, yo add the Bridge javascript interface to the webview, then make a few javascript calls to tell the javascript to redirect calls to the Bridge. Then you log in, which causes you _login_callback method to be executed and, consequently, your _login_status_chagned_callback to be called. That a summary? Commented Feb 20, 2012 at 13:18
  • You are following nicely, thats entirely correct. Commented Feb 20, 2012 at 13:30
  • 1
    To debug further, could you try not passing any params back to the callback? Also make sure the callback is not returning a value (which could be another source of this issue.) Commented Feb 20, 2012 at 14:46
  • Running this on my ICS phone, instead of the 2.2 emulator i get the following message: "Running NPMethod on non-NPObject"... Does that mean anything to anyone? Commented Feb 20, 2012 at 14:46
  • Hmm... Calling _on_login_status_change with no arguments (and naturally, changing OnLoginStatusChange to not receive any arguments) did not have any effect... However, calling the onLoginStatusChange directly worked just fine... Could it be that android has issues storing a javascriptInterface method as a js variable? Commented Feb 20, 2012 at 14:52

1 Answer 1

4

Apparently, the webview has issues when you try to pass native functions as callbacks to other methods. What I did was create a new .js file which works as callbacks, all the js callbacks do, is calling the native method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.