【问题标题】:output for System.out.print("Hello") in Android programAndroid 程序中 System.out.print("Hello") 的输出
【发布时间】:2012-09-24 03:40:32
【问题描述】:

我想在我的 android 程序中查看任何变量 (String var) 的值。 您可以说是出于调试目的。

当我使用 System.out.print("Hello") 打印任何内容时 然后我在任何地方都找不到这个输出。

有谁知道在哪里可以找到这个输出。

这是我的代码-

package com.test1.nus;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        System.out.print("Hello");

        ....
     }

【问题讨论】:

    标签: java android


    【解决方案1】:

    默认情况下,Android 系统会将stdout (System.out) 输出重定向到/dev/null,这意味着您的消息丢失了。

    相反,在 Android 中记录调试字符串的常见模式如下

    import android.util.Log;
    

    然后在你的班级顶部YourClass

    private static final String TAG = YourClass.class.getSimpleName();
    

    要记录调试字符串,您需要调用

    Log.d(TAG, "your debug text here");
    

    在你的情况下会导致

    package com.test1.nus;
    
    import android.util.Log;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        private static final String TAG = MainActivity.class.getSimpleName();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Log.d(TAG, "Hello");
    
            ....
        }
    

    你终于可以在 Eclipse 中看到你的调试字符串了

    Windows > Show view > Other 并选择LogCat

    如果需要,按YourClass 标签过滤。

    但是,如果您确实需要查看 System.out.println 编写的消息,您需要告诉 Android 通过以下 shell 命令将它们路由到 logcat

    $ adb shell stop
    $ adb shell setprop log.redirect-stdio true
    $ adb shell start
    

    然后您将能够通过 LogCat 视图和标签 stdout 在 Eclipse 中看到您的调试消息。

    您可以从这里http://developer.android.com/tools/debugging/debugging-log.html的官方文档中获取更多详细信息

    【讨论】:

      【解决方案2】:

      您的输出将记录到logcat

      假设你使用的是eclipse:

      Window > Show View ---> Logcat (If this not visible, select other--Android--Logcat)
      

      【讨论】:

      • 您好 Nambari,我在 logcat 中找不到输出字符串。我需要更改任何设置吗?
      • 不,我认为您不需要进行任何设置更改(除了确保在 logcat 窗口中没有设置过滤器)。如果您的代码正在执行,您应该会看到 System.out 语句。
      【解决方案3】:

      查看此链接:http://www.droidnova.com/debugging-in-android-using-eclipse,541.html 它将显示一些与 logcat 相关的屏幕截图。在那里你可以找到你的输出信息。

      关注这个:http://developer.android.com/tools/help/logcat.html

      【讨论】:

        【解决方案4】:

        如果您想在 android 模拟器中检查您的输出,请使用 Toast messages

        system.out.println("....") 的结果将显示在logcat

        要签入 android emulator/device.just 这样做

        Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
        

        【讨论】:

          【解决方案5】:

          我建议你看看android.util.log 类。 Android 开发者页面有一个good introduction 用于使用它。 (其实我今天才发现这个,因为我是自己学安卓编程的。)

          【讨论】:

            【解决方案6】:

            如果你使用 AVD 会给你带来比仅仅在日志中打印更有趣的体验。 Here他们完美地描述了所有这些。

            【讨论】:

              【解决方案7】:

              如果出于调试目的,最好使用 Logs。

              有多种方法可用,例如 Log.v() Log.d() Log.i() Log.w() 和 Log.e() 分别用于详细、调试、信息、警告和错误日志。

              然后就可以在调试应用的时候查看logcat了。

              如需进一步参考,请转至here

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2022-01-10
                • 2020-11-02
                • 2015-01-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-04-17
                相关资源
                最近更新 更多