【问题标题】:Creating a simple output to logcat创建一个简单的输出到 logcat
【发布时间】:2013-03-15 06:17:37
【问题描述】:

我不知道输出 int logcat 的正确方法,api document 对我来说没有意义。

我觉得应该这样做:

package com.example.conflip;

import java.util.Random;

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

public class MainActivity extends Activity {

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

    public int flip() {
        Random randomNumber = new Random();
        int outcome = randomNumber.nextInt(2);
        Log.d(outcome);
        return outcome;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

但是我只收到错误The method d(String, String) in the type Log is not applicable for the arguments (int)

我需要将 int 转换为字符串吗?如果有,怎么做?

更新:

虽然以下所有解决方案都有效,但在我在 DDMS 中选择我的硬件设备之前,LogCat 不会显示输出。

【问题讨论】:

    标签: android logging int


    【解决方案1】:

    在onCreate方法之前添加这一行

    private static final String TAG = "your activity name";
    

    现在你在翻转

    Log.d(TAG, "outcome = " + outcome);
    

    【讨论】:

    • 我无法绕过这个错误Illegal modifier for parameter TAG; only final is permitted。将不胜感激。
    • 我认为问题不是拼写错误,因为我遇到了同样的错误。
    • TAG 应该被声明为类成员。如果是本地取出私人。
    • 您不要在代码中的任何地方调用 flip()。也许您的意思是在 setContentView 之后放置 flip()
    • 你是对的。但事实证明我没有选择用于记录的设备。 stackoverflow.com/questions/2250112/…
    【解决方案2】:

    使用Integer.toString(outcome),因为您需要String作为日志中的参数

    so overall Log.d(tag_name, Integer.toString(outcome));
    

    here可以查看Log的详细信息。

    【讨论】:

      【解决方案3】:

      使用 Log.d(字符串,字符串)。第一个字符串是一个将出现在 logcat 中的标签 - 一个您可以搜索的简单标识符。第二个是打印到日志的消息。要获取 int 的字符串,请使用 Integer.toString(value)。

      【讨论】:

        【解决方案4】:

        使用这个:

        public int flip() {
            Random randomNumber = new Random();
            int outcome = randomNumber.nextInt(2);
           Log.d("This is the output", outcome.toString());
            return outcome;
        }
        

        【讨论】:

          【解决方案5】:
          public class MainActivity extends Activity {
          
          private String TAG = "MainActivity"; //-------------Include this-----------
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  flip();   //----You miss this out perhaps-----
              }
          public int flip() {
              Random randomNumber = new Random();
              int outcome = randomNumber.nextInt(2);
              Log.d(TAG, "Checking Outcome Value:" +outcome); //----Include this--------
              return outcome;
          }
          

          你也可以把Log.d改成Log.i(信息)、Log.w(警告)、Log.e(错误)

          这取决于您要显示的消息类型(主要是颜色不同)。

          【讨论】:

          • 正如@Hoan Nguyen 所说,您没有在onCreate 中初始化flip();。因此,它没有经过 Log.d 进程。检查我编辑的答案
          【解决方案6】:

          您应该使用 string.valueof(integer) 来获取 log cat 中的输出,例如。

          int outcome = randomNumber.nextInt(2);
                  Log.d("urtag",String.valueOf(outcome));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-03-28
            • 1970-01-01
            • 1970-01-01
            • 2013-05-19
            • 2018-04-05
            • 2010-11-25
            • 2020-08-27
            相关资源
            最近更新 更多