我想在控制台打印一些东西,以便调试它。但由于某种原因,我的 Android 应用程序中没有打印任何内容。
那我该如何调试呢?
public class HelloWebview extends Activity {
WebView webview;
private static final String LOG_TAG = "WebViewDemo";
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new MyWebChromeClient());
webview.loadUrl("http://example.com/");
System.out.println("I am here");
}
最佳答案
更正:
在模拟器和大多数设备上,System.out.println
被重定向到 LogCat 并使用 Log.i()
打印。在非常旧的或自定义的 Android 版本上,这可能不是真的。
原文:
没有将消息发送到的控制台,因此 System.out.println
消息会丢失。同样,当您使用 javaw
运行“传统”Java 应用程序时,也会发生这种情况。
相反,您可以使用 Android Log
class :
Log.d("MyApp","I am here");
然后您可以在 Eclipse 的 Logcat View 中查看日志,或者通过运行以下命令:
adb logcat
养成查看 logcat 输出的习惯是很好的,因为这也是显示任何未捕获异常的堆栈跟踪的地方。
每个日志调用的第一个条目是标识日志消息来源的日志标记。这很有帮助,因为您可以过滤日志的输出以仅显示您的消息。为确保您与日志标签一致,最好将其定义为 static final String
某处。
Log.d(MyActivity.LOG_TAG,"Application started");
Log
中有5个单字母方法,分别对应以下级别:
e()
- 错误w()
- 警告i()
- 信息d()
- 调试v()
- 详细wtf()
- 多么可怕的失败
documentation says the following about the levels :
Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
关于java - 为什么 "System.out.println"在 Android 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2220547/