最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebView: Uncaught ReferenceError: Android is not defined - Stack Overflow

programmeradmin5浏览0评论

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 failed; clearing to background color.
I/chromium( 4564): [INFO:CONSOLE(1)] "Uncaught ReferenceError: Android is not defined", 
...

If I put the WebView in my XML layout, I don't get these errors. Notice that, if the script runs, it will change the onLoadEvent field value from "no" to "yes. That is happening, so evidently the script is running. It is not fetching the user name and password, though, indicating that "Android" is not defined.

I have also tried executing addView() before webView.loadData(); same errors.

Here's the code that creates the WebView:

  @SuppressLint("SetJavaScriptEnabled")
  private void onOk ()
  {
    Log.d ("RegTest", "onOk");
    webView = new WebView (this);
    webView.setLayoutParams (new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT));    
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled (true);
    CharSequence cs = readAsset ("input_form");
    webView.loadData (cs.toString(), "text/html", "UTF-8");

    contentView.addView (webView);
  }

And here are snips of "input_form", which is the source for the WebView content:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">

<head>
  ...
  <script type="text/javascript">
    function onLoadValues ()
    {
      document.getElementById ("onLoadEvent").value = "yes";
      document.getElementById ("FullName").value = Android.getFullName();
      document.getElementById ("EmailAddress").value = Android.getEmailAddr(); 
    }
  </script>
</head>

<body onload="onLoadValues()">
  <form name="catwebformform52174" method="post" ...>
    <fieldset>
      <div class="pure-control-group">
        <label for="onLoadValues">onLoadEvent</label>
        <input id="onLoadEvent" name="onLoadEvent" type="text" placeholder="no">
      </div>
      <div class="pure-control-group">
        <label for="FullName">Name</label>
        <input id="FullName" name="FullName" type="text" placeholder="Name">
      </div>
      <div class="pure-control-group">
        <label for="EmailAddress">Email Address</label>
        <input id="EmailAddress" name="EmailAddress" type="email" placeholder="Email Address">
      </div>
    </fieldset>
  </form>
</body>
</html>

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 failed; clearing to background color.
I/chromium( 4564): [INFO:CONSOLE(1)] "Uncaught ReferenceError: Android is not defined", 
...

If I put the WebView in my XML layout, I don't get these errors. Notice that, if the script runs, it will change the onLoadEvent field value from "no" to "yes. That is happening, so evidently the script is running. It is not fetching the user name and password, though, indicating that "Android" is not defined.

I have also tried executing addView() before webView.loadData(); same errors.

Here's the code that creates the WebView:

  @SuppressLint("SetJavaScriptEnabled")
  private void onOk ()
  {
    Log.d ("RegTest", "onOk");
    webView = new WebView (this);
    webView.setLayoutParams (new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT));    
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled (true);
    CharSequence cs = readAsset ("input_form");
    webView.loadData (cs.toString(), "text/html", "UTF-8");

    contentView.addView (webView);
  }

And here are snips of "input_form", which is the source for the WebView content:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">

<head>
  ...
  <script type="text/javascript">
    function onLoadValues ()
    {
      document.getElementById ("onLoadEvent").value = "yes";
      document.getElementById ("FullName").value = Android.getFullName();
      document.getElementById ("EmailAddress").value = Android.getEmailAddr(); 
    }
  </script>
</head>

<body onload="onLoadValues()">
  <form name="catwebformform52174" method="post" ...>
    <fieldset>
      <div class="pure-control-group">
        <label for="onLoadValues">onLoadEvent</label>
        <input id="onLoadEvent" name="onLoadEvent" type="text" placeholder="no">
      </div>
      <div class="pure-control-group">
        <label for="FullName">Name</label>
        <input id="FullName" name="FullName" type="text" placeholder="Name">
      </div>
      <div class="pure-control-group">
        <label for="EmailAddress">Email Address</label>
        <input id="EmailAddress" name="EmailAddress" type="email" placeholder="Email Address">
      </div>
    </fieldset>
  </form>
</body>
</html>
Share Improve this question asked Mar 14, 2015 at 21:01 Peri HartmanPeri Hartman 19.5k18 gold badges59 silver badges109 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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(Object object, 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*

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:

class JsObject {
  @JavascriptInterface
  public String toString() { return "injectedObject"; }
}
webview.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");

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

发布评论

评论列表(0)

  1. 暂无评论