为什么没有“System.out.println"?在安卓上工作

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

问题描述

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

那我该如何调试呢?

公共类 HelloWebview 扩展 Activity {WebView 网络视图;私有静态最终字符串 LOG_TAG = "WebViewDemo";私有类 HelloWebViewClient 扩展 WebViewClient {@覆盖public boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);返回真;}}/** 在第一次创建活动时调用.*/@覆盖公共无效 onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);设置内容视图(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中有5个单字母方法,分别对应以下级别:

  • e() - 错误
  • w() - 警告
  • i() - 信息
  • d() - 调试
  • v() - 详细
  • wtf() - 多么可怕的失败

文档对级别进行了如下说明:

<块引用>

除非在开发过程中,否则绝不应将详细信息编译到应用程序中.调试日志被编译,但在运行时被剥离.始终保留错误、警告和信息日志.

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"?在安卓上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:什么是用于终端应用程序的好的 Java、curses 类库 下一篇:如何将控制台输出写入 txt 文件

相关文章

最新文章