Android: problem with debugging javascript in webview-ThrowExceptions

Exception or error:

I have some javascript running in WebView, and I want to receive console messages about errors in JS.
In accordance with the instructions at http://developer.android.com/guide/webapps/debugging.html I overrided methods

WebChromeClient.onConsoleMessage(String message, int lineNumber, String sourceID)

and

WebChromeClient.onConsoleMessage(ConsoleMessage cm),

redirecting messages to logcat. In android 2.1 it works well, but in android 2.2 none of this methods are called.

What I am doing wrong?

Thanks.

How to solve:

It seems that HTC disabled console.log on Android 2.2 devices. check out this post:

How to display console.log() output in PhoneGap app using Eclipse and HTC Desire HD?

###

I just tested on 2.2 (level 8). It’s working fine. Not sure why you’re not seeing it. I would validate the setting of ChromeClient.

js contains…

console.log("Hello World");
console.error("Serious");

ChromeClient contains

    @Override
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    // TODO Auto-generated method stub
    Log.v("ChromeClient", "invoked: onConsoleMessage() - " + sourceID + ":"
            + lineNumber + " - " + message);
    super.onConsoleMessage(message, lineNumber, sourceID);
}

@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
    Log.v("ChromeClient", cm.message() + " -- From line "
            + cm.lineNumber() + " of "
            + cm.sourceId() );
    return true;
}

Log contains
03-16 15:53:12.309: VERBOSE/ChromeClient(595): Hello World — From line 24 of file:///android_asset/base.js
03-16 15:53:12.309: VERBOSE/ChromeClient(595): Serious — From line 25 of file:///android_asset/base.js

Leave a Reply

Your email address will not be published. Required fields are marked *