Skip to content Skip to sidebar Skip to footer

Webview: Uncaught Referenceerror: Android Is Not Defined

I am creating a WebView with code rather than loading via an XML layout. The webview appears to be created properly, but I am seeing the errors: W/AwContents( 4564): nativeOnDraw

Solution 1:

You didn't defined Android as a javascripinterface you have to write below lines

// If your callback methods are in same class then just same class object
 webView.addJavascriptInterface(this, "Android");

Syntax of addJavascriptInterface() method

      addJavascriptInterface(Objectobject, String name);        
   //  1. `object` – *the Java object to inject into this WebView’s JavaScript context.*//  2. `name` – *the name used to expose the object in JavaScript*

Solution 2:

If you are here because you were just following the walk through documentation they leave off a key piece of information that is only available on the reference documentation for addJavascriptInterface().

Note that injected objects will not appear in JavaScript until the page is next (re)loaded. JavaScript should be enabled before injecting the object. For example:

classJsObject {
  @JavascriptInterfacepublicStringtoString() { return"injectedObject"; }
}
webview.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(newJsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");

https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface(java.lang.Object,%20java.lang.String)

Post a Comment for "Webview: Uncaught Referenceerror: Android Is Not Defined"