我一直在使用三款不同的 HTC 手机,几乎完全是,从来没有遇到过这个问题。这里有几件事要检查:
- 确保启用了 USB 调试。
- 确保您的 Webview 设置了 WebChromeClient。Webview 中使用的浏览器没有实现console.log()。
- 应该很容易看到 adb logcat 的输出,但为了更容易,过滤输出。
开启USB调试:
- 断开设备与计算机的连接。
- 转到设置 -> 应用程序 -> 开发 -> 选择“启用 USB 调试”
- 电脑插件。(确保您安装了正确的驱动程序以使用 ADB - 更多信息在这里:http: //developer.android.com/guide/developing/device.html)
设置一个覆盖 onConsoleMessage() 的 WebChromeClient:
//Set the output prefix so you can filter adb logcat results later
public static final String TAG = "Your-Application-Name";
myWebView = (WebView) findViewById(R.id.webview);
//Not going to have much luck running JS without this:
myWebView.getSettings().setJavaScriptEnabled(true);
//Override onConsoleMessage() to output to the Log.
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
Log.d(TAG, cm.message() + " -- From line "
+ cm.lineNumber() + " of "
+ cm.sourceId() );
return true;
}
});
关于 onConsoleMessage() 的更多信息:http://developer.android.com/reference/android/webkit/WebChromeClient.html#onConsoleMessage(java.lang.String , int, java.lang.String)
有关一般调试的更多信息:http: //developer.android.com/guide/webapps/debugging.html
过滤 adb logcat 的输出:
adb logcat tag-name:log-level *:S
tag-name 匹配 Log.x 中指定的字符串 log-level 匹配调用 Log.x 时指定的日志级别 <---
与上述代码相关的示例:
adb logcat Your-Application-Name:D *:S
这将显示匹配标签 Your-Application-Name 的所有 d 级别日志,并静音其他所有内容。
有关 adb 过滤的更多信息:http:
//developer.android.com/guide/developing/tools/adb.html#logcat
希望能帮助到你!我知道这是对其他答案的一些总结,但事实是,它需要所有步骤才能使其发挥作用。:)