Reputation: 5016
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");
}
Upvotes: 0
Views: 958
Reputation: 5016
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.
Upvotes: 4