javascript WebView:未捕获的 ReferenceError:Android 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29054152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:01:36  来源:igfitidea点击:

WebView: Uncaught ReferenceError: Android is not defined

javascriptandroidwebview

提问by Peri Hartman

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:

我正在使用代码创建一个 WebView,而不是通过 XML 布局加载。webview 似乎创建正确,但我看到错误:

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.

如果我将 WebView 放在我的 XML 布局中,我不会收到这些错误。请注意,如果脚本运行,它会将 onLoadEvent 字段值从“no”更改为“yes “ 没有定义。

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

我也试过在 webView.loadData(); 之前执行 addView(); 同样的错误。

Here's the code that creates the WebView:

这是创建 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:

这里是“input_form”的片段,它是 WebView 内容的来源:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/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>

回答by IshRoid

You didn't defined Androidas a javascripinterfaceyou have to write below lines

您没有将Android定义为javascripinterface必须在以下几行中写入

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

Syntax of addJavascriptInterface() method

addJavascriptInterface() 方法的语法

      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*