![](/img/trans.png)
[英]Android Webview Uncaught ReferenceError: $ is not defined
[英]WebView: Uncaught ReferenceError: Android is not defined
我正在使用代码创建一个 WebView,而不是通过 XML 布局加载。 webview 似乎创建正确,但我看到错误:
W/AwContents( 4564): nativeOnDraw failed; clearing to background color.
I/chromium( 4564): [INFO:CONSOLE(1)] "Uncaught ReferenceError: Android is not defined",
...
如果我将 WebView 放在我的 XML 布局中,我不会收到这些错误。 请注意,如果脚本运行,它会将 onLoadEvent 字段值从“no”更改为“yes “ 没有定义。
我也试过在 webView.loadData(); 之前执行 addView(); 同样的错误。
这是创建 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);
}
这里是“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>
您没有将Android定义为javascripinterface
您必须在行下面写
// If your callback methods are in same class then just same class object
webView.addJavascriptInterface(this, "Android");
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*
如果您来到这里是因为您只是按照文档进行addJavascriptInterface()
,他们会遗漏一个关键信息,该信息仅在addJavascriptInterface()
的参考文档中addJavascriptInterface()
。
请注意,在下一次(重新)加载页面之前,注入的对象不会出现在 JavaScript 中。 在注入对象之前应该启用 JavaScript。 例如:
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())");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.