在WebView上启用Javascript。因为没有JavaScipt网页图像缩放无法正常工作。这是我的代码,我如何在WebView中加载url:Android - WebView在调用JavaScript函数时崩溃应用程序
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String url = "http://nuz.uz/ekonomika-i-finansy/18651-v-2017-godu-v-uzbekistane-prekratitsya-realizaciya-lampochek-ilicha.html";
final WebView webview = (WebView) findViewById(R.id.webview);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("Tag", message + " -- From line "
+ lineNumber + " of "
+ sourceID);
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String loadingUrl) {
if (loadingUrl.equals(url)) return false;//open news link in webview
else {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(loadingUrl));
startActivity(browserIntent);
return true;
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
Log.d("Tag", "Error occured: "+error.toString());
}
});
webview.loadUrl(url);
}
}
当我运行应用程序时,它正在打开网页。加载页面的所有内容后,应用程序崩溃。我试图表明误差在控制台上它显示以下错误:
12-14 02:42:06.936 25760-25760/? D/Tag: Uncaught TypeError: Cannot call method 'init' of undefined -- From line 951 of http://nuz.uz/ekonomika-i-finansy/18651-v-2017-godu-v-uzbekistane-prekratitsya-realizaciya-lampochek-ilicha.html
我已经检查URL的内容,发现以下,这是造成应用程序崩溃:
<script type="text/javascript">
jQuery(document).ready(function() {
App.init();
});
</script>
line 951: App.init();
我怎样才能解决这个问题不关闭JavaScript?如何防止WebView崩溃应用程序?
不幸的是,这不是我的网站。我无法控制它=)有其他解决方法吗? –
你将不得不a)加载没有JS的网站,b)进入并修复错误(不知道你是否可以混淆加载的HTML)然后c)启用JS。在我的书中不可行或不实际。试试这可能:http://stackoverflow.com/questions/32530228/android-api-23-webview-how-to-ignore-javascript-errors –