如何在Android应用程序的WebView中看到Javascript错误? [英] How can I see Javascript errors in WebView in an Android app?
问题描述
我正在尝试在应用程序的WebView中运行javascript.我正在Nexus 7上进行开发.
I'm trying to run javascript in WebView in an app. I'm developing on Nexus 7.
html/javascript在Chromium上可以正常工作,但某些操作未在平板电脑上进行.有没有办法查看平板电脑上的任何JavaScript本身是否失败?一种控制台视图吗?
The html / javascript works fine on Chromium, but certain actions aren't happening on the tablet. Is there a way of seeing if any of the javascript itself is failing on the tablet? A kind of console view?
推荐答案
您实际上可以从WebView接收控制台消息,从而可以捕获其引发的错误.
You can actually receive the console messages from a WebView, which would allow you to catch the errors that it throws.
要这样做:
- 在WebView上启用JavaScript
- 设置 WebChromeClient
- 覆盖 onConsoleMessage
示例:
final WebView webView = (WebView) findViewById(R.id.webview_terms_conditions);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.d("MyApplication", consoleMessage.message() + " -- From line "
+ consoleMessage.lineNumber() + " of "
+ consoleMessage.sourceId());
return super.onConsoleMessage(consoleMessage);
}
});
webView.loadUrl(getString(R.string.url_terms_conditions));
类似于此处,尽管该文档不是完成并且使用了不推荐使用的方法.
Similar to what it says here, though that doc isn't complete and it uses a deprecated method.
在Android KitKat上运行时,您还可以启用远程调试!
When running on Android KitKat, you can also enable remote debugging!
这篇关于如何在Android应用程序的WebView中看到Javascript错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!