使用Eclipse的Android dev等效的NSLog

Bla*_*oil 13 android

我想知道是否有一种语法可以在eclipse控制台窗口中打印出来.这就像xcode中的NSLog一样.谢谢

小智 18

使用http://developer.android.com/reference/android/util/Log.htmlandroid.util.Log类.

例如:

import android.util.Log;

class Someclass {
    private static final String TAG = "Someclass";
    ...
    public boolean someMethod(int argument) {
        Log.i(TAG, "This is some information log");
        if (argument == 0) {
           Log.e(TAG, "Error argument is 0!!!");
           return false;
        }


        Log.w(TAG, "Warning returning default value");
        return true;
    }
};
Run Code Online (Sandbox Code Playgroud)

之所以分配TAG变量而不是像以下那样: Somelcass.class.getSimpleName()是因为反射方法会导致在初始化时加载类的反射元数据,然而,android开发人员的"首选"方法会阻止它,从而节省CPU和初始化时间.