Java Android Webview 不适用于 Android 4.4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21437270/
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
Android Webview not working on Android 4.4
提问by user3241778
I am developing an Android application based on a Webview. All is working fine on Android 4.1 but on 4.4 it throws this errors.
我正在开发基于 Webview 的 Android 应用程序。在 Android 4.1 上一切正常,但在 4.4 上会引发此错误。
01-29 11:16:03.075: E/eglCodecCommon(2179): glUtilsParamSize: unknow param 0x00000bd0 01-29 11:16:03.095: E/eglCodecCommon(2179): ** ERROR unknown type 0x10037 (glSizeof,72)
01-29 11:16:03.075: E/eglCodecCommon(2179): glUtilsParamSize: 未知参数 0x00000bd0 01-29 11:16:03.095: E/eglCodecCommon(2179), *7x10g 未知类型: *7x10g
If I uncomment this line it' working but (logically) not showing anything
如果我取消注释这一行,它就可以工作,但(逻辑上)没有显示任何内容
webview.loadUrl("file:///android_asset/html/index.html");
The Webview config code looks like this.
Webview 配置代码如下所示。
webview = new WebView(this);
webview.clearCache(true);
webview.clearHistory();
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient());
//webview.setWebChromeClient(new WebChromeClient());
webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");
setContentView(webview);
webview.loadUrl("file:///android_asset/html/index.html");
The HTML code is exactly
HTML代码正是
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<meta name="description" content="">
<meta name="author" content="">
<title>My Web App</title>
<script src="../js/jquery.js"></script>
<script src="../js/hammer.js"></script>
<script src="../js/lightboxes.js"></script>
<link href="../css/main.css" rel="stylesheet">
<link href="../css/lightboxes.css" rel="stylesheet">
<script type="text/javascript">
activeId = "contentOne";
passiveId = "contentTwo";
swiping = false;
function changeElements()
{
$("#" + passiveId).css("display", "none");
$("#" + passiveId).css("-webkit-transition", "-webkit-transform 0s linear");
$("#" + passiveId).css("-webkit-transform", "translate(150%,0)");
setTimeout(function() {
$("#" + passiveId).css("display", "block");
$("#" + passiveId).css("-webkit-transition", "-webkit-transform 0.25s ease-in-out");
swiping = false;
}, 10);
}
function nextPhrase()
{
if (!swiping)
{
swiping = true;
$("#" + activeId).css("-webkit-transform", "translate(-101%,0)");
$("#" + passiveId).css("-webkit-transform", "translate(2.5%,0)");
var result = Android.getPhrase();
document.getElementById(passiveId + "Text").textContent = result;
tempId = activeId;
activeId = passiveId;
passiveId = tempId;
setTimeout(changeElements, 600);
}
//$("#"+passiveId).css("-webkit-transition", "-webkit-transform 0.1s linear");
//$("#"+passiveId).css("-webkit-transform", "translate(-100%,0)");
//$("#"+passiveId).css("-webkit-transition", "-webkit-transform 0.6s ease-in-out");
//document.getElementById("next").style.backgroundColor="blue";
}
function rel()
{
document.getElementById("next").style.backgroundColor = "#FFF";
}
function changeColor(el)
{
document.getElementById("next").style.backgroundColor = "#EEE";
}
</script>
</head>
<body>
<div id="contentHeader">
Never I have ever ...
</div>
<div id="container">
<div id="contentOne">
<div id="contentOneText">
</div>
</div>
<div id="contentTwo">
<div id="contentTwoText">
</div>
</div>
</div>
<div id="headNav">
<div class="navElement" id="next" >
<div class="elName">
Next
</div>
</div>
</div>
<div id="proposalLightbox">sdfsdfasfasdf</div>
<div id="fade"></div>
<script>
Hammer(document).on("dragleft", function(e) {
nextPhrase();
});
document.getElementById('next').ontouchstart = function(eve) {
nextPhrase();
document.getElementById("next").style.backgroundColor = "#EEE";
}
document.getElementById('next').ontouchend = function(eve) {
//alert()
document.getElementById("next").style.backgroundColor = "#FFF";
}
var result = Android.getPhrase();
document.getElementById(activeId + "Text").textContent = result;
function testEcho(par)
{
alert(par);
}
function openProposeDialog(par)
{
showLightbox("proposalLightbox");
}
document.getElementById('fade').ontouchstart = function(eve) {
closeLightboxes();
}
//nextPhrase();
</script>
</body>
</html>
And the Java-Javascript interface is
而 Java-Javascript 接口是
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
public String showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
return toast;
}
public String getPhrase() {
String phrase = "";
//phrase = dataHandler.getRandomPhrase();
return phrase;
}
}
Thanks a lot in advance
非常感谢提前
回答by rajahsekar
please check new kitkat api web view functionallity has changed
请检查新的 kitkat api web 视图功能已更改
回答by chank007
Check with it.
检查它。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mWebView.evaluateJavascript("function myFunc(){"Your javascript code"};);", new ValueCallback<String>() {
@Override
public void onReceiveValue(final String dataResult) {
}
});
} else {
mWebView.loadUrl("function myFunc(){"Your javascript code"};);");
}