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

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

问题描述

我想在控制台打印一些东西,以便我可以调试它.但出于某种原因,我的 Android 应用程序中没有打印任何内容.

那我该如何调试?

public class HelloWebview extends Activity {WebView 网页视图;私有静态最终字符串LOG_TAG =WebViewDemo";私有类 HelloWebViewClient 扩展了 WebViewClient {@覆盖public boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);返回真;}}/** 在第一次创建活动时调用.*/@覆盖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("我来了");}

解决方案

更正:

在模拟器和大多数设备上,System.out.println 被重定向到 LogCat 并使用 Log.i() 打印.这可能不适用于非常旧的或自定义的 Android 版本.

原文:

没有控制台可以发送消息,因此 System.out.println 消息会丢失.同样,当您使用 javaw 运行传统"Java 应用程序时,也会发生这种情况.

相反,您可以使用

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

Log.d(MyActivity.LOG_TAG,"应用程序启动");

Log中有五种单字母方法,分别对应以下级别:

  • e() - 错误
  • w() - 警告
  • i() - 信息
  • 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"没有在安卓上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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