为什么没有“System.out.println”在Android中工作? [英] Why doesn't "System.out.println" work in Android?

查看:160
本文介绍了为什么没有“System.out.println”在Android中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制台中打印东西,以便我可以调试它。但由于某些原因,在我的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;
}
}

/ **在首次创建活动时调用。 * /
@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);
}


解决方案

/ strong>


在模拟器和大多数设备上 System.out.println 被重定向到LogCat并使用 Log.i ()。这可能不是真的在很老的或自定义Android版本。



原始:


没有控制台发送消息,因此系统。 out.println 消息丢失。同样,当你使用 javaw 运行一个传统的Java应用程序时也会发生这种情况。



请使用



每个记录调用的第一个条目是标记源的日志标记的日志消息。这是有帮助的,因为您可以过滤日志的输出以仅显示您的消息。要确保您与日志标记一致,最好将其定义为 static final String 某处。

  Log.d(MyActivity.LOG_TAG,Application started); 

Log 对应于以下级别:




  • e() - 错误

  • w() - 警告

  • i $ c> - 信息

  • d() - 调试

  • > v() - 详细

  • wtf() - 可怕的失败



文档说明了关于级别的以下内容:


除非在开发期间,否则不应将Verbose编译到应用程序中。调试日志在编译时在运行时被剥离。错误,警告和信息日志始终保留。



I want to print something in console, so that I can debug it. But for some reason, nothing prints in my Android application.

How do I debug then?

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");
    }

解决方案

Correction:

On the emulator and most devices System.out.println gets redirected to LogCat and printed using Log.i(). This may not be true on very old or custom Android versions.

Original:

There is no console to send the messages to so the System.out.println messages get lost. In the same way this happens when you run a "traditional" Java application with javaw.

Instead, you can use the Android Log class:

Log.d("MyApp","I am here");

You can then view the log either in the Logcat view in Eclipse, or by running the following command:

adb logcat

It's good to get in to the habit of looking at logcat output as that is also where the Stack Traces of any uncaught Exceptions are displayed.

The first Entry to every logging call is the log tag which identifies the source of the log message. This is helpful as you can filter the output of the log to show just your messages. To make sure that you're consistent with your log tag it's probably best to define it once as a static final String somewhere.

Log.d(MyActivity.LOG_TAG,"Application started");

There are five one-letter methods in Log corresponding to the following levels:

  • e() - Error
  • w() - Warning
  • i() - Information
  • d() - Debug
  • v() - Verbose
  • wtf() - What a Terrible Failure

The 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.

这篇关于为什么没有“System.out.println”在Android中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆