2013-03-15 63 views
0

我找不出輸出int logcat的正確方法,並且api document對我來說沒有意義。創建一個簡單的輸出到logcat

我覺得這應該這樣做:

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)

我是否需要整型轉換爲字符串?如果是這樣,怎麼樣?

更新:

雖然下面的所有解決方案做的工作,logcat的不會,直到我選擇我的硬件設備DDMS顯示輸出。

回答

2

使用Integer.toString(outcome),因爲你需要String作爲參數登錄

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

here你可以看到日誌的詳細信息。

+0

好吧,我沒有得到一個錯誤,但我不logcat中看到任何要麼... – nipponese 2013-03-15 06:43:50

1

使用Log.d(String,String)。第一個字符串是一個將出現在logcat中的標籤 - 一個您可以搜索的簡單標識符。第二個是打印到日誌的消息。要獲取int的字符串,請使用Integer.toString(value)。

2

onCreate方法

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

前加入這一行,現在你在翻蓋

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

我不能左右這個錯誤'爲參數TAG非法修改搞定;只有最後被允許。一個解釋將不勝感激。 – nipponese 2013-03-15 06:24:10

+0

我拼錯的字符串,我只是編輯它。 – 2013-03-15 06:25:37

+0

我不認爲問題是拼寫錯誤,因爲我得到相同的錯誤。 – nipponese 2013-03-15 06:27:59

1

使用此:

public int flip() { 
    Random randomNumber = new Random(); 
    int outcome = randomNumber.nextInt(2); 
    Log.d("This is the output", outcome.toString()); 
    return outcome; 
} 
1
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(錯誤)

這取決於您想要顯示哪種類型的消息(主要是顏色不同)。

+0

沒有錯誤,我只是不明白爲什麼LogCat是空白的。 – nipponese 2013-03-15 06:55:39

+0

正如@Hoan Nguyen所說的,你沒有初始化你的onCreate中的'flip();'。因此,它沒有通過Log.d進程。檢查我編輯的答案 – 2013-03-15 06:58:30

0

您應該使用string.valueof(整數)來獲取log cat中的輸出,例如。

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