Android/Java 调试打印堆栈信息的多种方法

//方法1:
try {
    new Exception("print trace").printStackTrace();
} catch(Exception e) {
    e.printStackTrace();
    android.util.Log.e("TestTag", "Exception:" + e, e);
}
//方法2:
RuntimeException here = new RuntimeException("bolex");
here.fillInStackTrace();
here.printStackTrace();
Log.w("myTag", "Called: " + this, here);
//方法3:
try {
    Object mObject = null;
    mObject.toString();
} catch (Exception e) {
    e.printStackTrace();
    android.util.Log.e("TestTag", "NPE:" + e, e);
}
//方法4:
for (StackTraceElement i : Thread.currentThread().getStackTrace()) {
    Log.d(TAG, "-- ↑ --" + i.toString());
}
//方法5:
Log.w(TAG, "stack", new Exception());
//方法6:
Log.d(TAG, Log.getStackTraceString(new Throwable()));
//方法7:
android.util.Log.e("MyTag", Thread.currentThread().getStackTrace()[2].getClassName()+"-->"+Thread.currentThread().getStackTrace()[2].getMethodName()+"()-->"+Thread.currentThread().getStackTrace()[2].getLineNumber());

猜你喜欢

转载自blog.csdn.net/zhangqi6627/article/details/106240191