0

logcat に int を出力する適切な方法がわかりません。APIドキュメントは意味がありません。

私はこれがそれを行うべきだと感じます:

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 は出力を表示しません。

4

6 に答える 6

2

onCreateメソッドの前にこの行を追加します

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

そして今、あなたはひっくり返ります

Log.d(TAG, "outcome = " + outcome);
于 2013-03-15T06:20:45.800 に答える
2

Integer.toString(outcome)ログのパラメータとして文字列が必要なときに使用します

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

ここでは、ログの詳細を確認できます。

于 2013-03-15T06:19:51.603 に答える
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(エラー)に変更することもできます

これは、表示するメッセージの種類によって異なります(主に色が異なります)。

于 2013-03-15T06:53:04.913 に答える
1

Log.d(String, String) を使用します。最初の文字列は、logcat に表示されるタグで、検索できる簡単な識別子です。2 番目は、ログに出力されるメッセージです。int の文字列を取得するには、Integer.toString(value) を使用します。

于 2013-03-15T06:20:13.453 に答える
1

これを使って:

public int flip() {
    Random randomNumber = new Random();
    int outcome = randomNumber.nextInt(2);
   Log.d("This is the output", outcome.toString());
    return outcome;
}
于 2013-03-15T06:30:02.203 に答える
0

たとえば、log cat で出力を取得するには、string.valueof(integer) を使用する必要があります。

int outcome = randomNumber.nextInt(2);
        Log.d("urtag",String.valueOf(outcome));
于 2013-03-15T06:56:29.073 に答える