在Android调试过程中经常会出现程序出现的结果不是我们预期的结果,那就需要加Log打印调试,看调用过程是否正确,此时就需要打印程序的调用栈,特别是Android代码相当庞大,打印堆栈更有利于我们分析问题,下面就记录下平时用到不退出程序打印堆栈的方法。
验证的方法相关简单,在Activity的类中创建一个方法ThrowException,在onCreate中调用此方法,看打印出什么
public void ThrowException() {
// 调试打印堆栈而不退出
Log.d(TAG, Log.getStackTraceString(new Throwable()));
// 创建异常打印堆栈
Exception e = new Exception("this is a log");
e.printStackTrace();
// 获取当前线程的堆栈
for (StackTraceElement i : Thread.currentThread().getStackTrace()) {
Log.i(TAG, i.toString());
}
RuntimeException re = new RuntimeException();
re.fillInStackTrace();
Log.i(TAG, "stackTrace", re);
// 主动抛出异常调试
try {
Log.i(TAG,
"--------------------------------NullPointerException-----------1");
throw new NullPointerException();
} catch (NullPointerException e1) {
// TODO: handle exception
Log.i(TAG, "--------------------------------NullPointerException");
Log.e(TAG, Log.getStackTraceString(e1));
// e1.printStackTrace();
}
Log.i(TAG,
"--------------------------------NullPointerException-----------end");
}
1、Log.d(TAG, Log.getStackTraceString(new Throwable()));
04-09 14:19:51.764 D/dzt_test(26317): java.lang.Throwable
04-09 14:19:51.764 D/dzt_test(26317): at com.dzt.testapp.MainActivity.ThrowException(MainActivity.java:69)
04-09 14:19:51.764 D/dzt_test(26317): at com.dzt.testapp.MainActivity.onCreate(MainActivity.java:63)
04-09 14:19:51.764 D/dzt_test(26317): at android.app.Activity.performCreate(Activity.java:5343)
04-09 14:19:51.764 D/dzt_test(26317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-09 14:19:51.764 D/dzt_test(26317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
04-09 14:19:51.764 D/dzt_test(26317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
04-09 14:19:51.764 D/dzt_test(26317): at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-09 14:19:51.764 D/dzt_test(26317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
04-09 14:19:51.764 D/dzt_test(26317): at android.os.Handler.dispatchMessage(Handler.java:110)
04-09 14:19:51.764 D/dzt_test(26317): at android.os.Looper.loop(Looper.java:193)
04-09 14:19:51.764 D/dzt_test(26317): at android.app.ActivityThread.main(ActivityThread.java:5333)
04-09 14:19:51.764 D/dzt_test(26317): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 14:19:51.764 D/dzt_test(26317): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 14:19:51.764 D/dzt_test(26317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
04-09 14:19:51.764 D/dzt_test(26317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
04-09 14:19:51.764 D/dzt_test(26317): at dalvik.system.NativeStart.main(Native Method)
2、
Exception e = new Exception("this is a log");
e.printStackTrace();
04-09 14:19:51.764 W/System.err(26317): java.lang.Exception: this is a log
04-09 14:19:51.765 W/System.err(26317): at com.dzt.testapp.MainActivity.ThrowException(MainActivity.java:72)
04-09 14:19:51.765 W/System.err(26317): at com.dzt.testapp.MainActivity.onCreate(MainActivity.java:63)
04-09 14:19:51.765 W/System.err(26317): at android.app.Activity.performCreate(Activity.java:5343)
04-09 14:19:51.765 W/System.err(26317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-09 14:19:51.765 W/System.err(26317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
04-09 14:19:51.766 W/System.err(26317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
04-09 14:19:51.766 W/System.err(26317): at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-09 14:19:51.766 W/System.err(26317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
04-09 14:19:51.766 W/System.err(26317): at android.os.Handler.dispatchMessage(Handler.java:110)
04-09 14:19:51.766 W/System.err(26317): at android.os.Looper.loop(Looper.java:193)
04-09 14:19:51.766 W/System.err(26317): at android.app.ActivityThread.main(ActivityThread.java:5333)
04-09 14:19:51.766 W/System.err(26317): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 14:19:51.766 W/System.err(26317): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 14:19:51.766 W/System.err(26317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
04-09 14:19:51.766 W/System.err(26317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
04-09 14:19:51.767 W/System.err(26317): at dalvik.system.NativeStart.main(Native Method)
3、
// 获取当前线程的堆栈
for (StackTraceElement i : Thread.currentThread().getStackTrace()) {
Log.i(TAG, i.toString());
}
04-09 14:19:51.767 I/dzt_test(26317): dalvik.system.VMStack.getThreadStackTrace(Native Method)
04-09 14:19:51.768 I/dzt_test(26317): java.lang.Thread.getStackTrace(Thread.java:579)
04-09 14:19:51.768 I/dzt_test(26317): com.dzt.testapp.MainActivity.ThrowException(MainActivity.java:76)
04-09 14:19:51.768 I/dzt_test(26317): com.dzt.testapp.MainActivity.onCreate(MainActivity.java:63)
04-09 14:19:51.768 I/dzt_test(26317): android.app.Activity.performCreate(Activity.java:5343)
04-09 14:19:51.769 I/dzt_test(26317): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-09 14:19:51.769 I/dzt_test(26317): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
04-09 14:19:51.769 I/dzt_test(26317): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
04-09 14:19:51.769 I/dzt_test(26317): android.app.ActivityThread.access$800(ActivityThread.java:151)
04-09 14:19:51.770 I/dzt_test(26317): android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
04-09 14:19:51.770 I/dzt_test(26317): android.os.Handler.dispatchMessage(Handler.java:110)
04-09 14:19:51.770 I/dzt_test(26317): android.os.Looper.loop(Looper.java:193)
04-09 14:19:51.770 I/dzt_test(26317): android.app.ActivityThread.main(ActivityThread.java:5333)
04-09 14:19:51.771 I/dzt_test(26317): java.lang.reflect.Method.invokeNative(Native Method)
04-09 14:19:51.771 I/dzt_test(26317): java.lang.reflect.Method.invoke(Method.java:515)
04-09 14:19:51.771 I/dzt_test(26317): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
04-09 14:19:51.771 I/dzt_test(26317): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
04-09 14:19:51.771 I/dzt_test(26317): dalvik.system.NativeStart.main(Native Method)
4、
RuntimeException re = new RuntimeException();
re.fillInStackTrace();
Log.i(TAG, "stackTrace", re);
04-09 14:19:51.773 I/dzt_test(26317): stackTrace
04-09 14:19:51.773 I/dzt_test(26317): java.lang.RuntimeException
04-09 14:19:51.773 I/dzt_test(26317): at com.dzt.testapp.MainActivity.ThrowException(MainActivity.java:81)
04-09 14:19:51.773 I/dzt_test(26317): at com.dzt.testapp.MainActivity.onCreate(MainActivity.java:63)
04-09 14:19:51.773 I/dzt_test(26317): at android.app.Activity.performCreate(Activity.java:5343)
04-09 14:19:51.773 I/dzt_test(26317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-09 14:19:51.773 I/dzt_test(26317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
04-09 14:19:51.773 I/dzt_test(26317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
04-09 14:19:51.773 I/dzt_test(26317): at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-09 14:19:51.773 I/dzt_test(26317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
04-09 14:19:51.773 I/dzt_test(26317): at android.os.Handler.dispatchMessage(Handler.java:110)
04-09 14:19:51.773 I/dzt_test(26317): at android.os.Looper.loop(Looper.java:193)
04-09 14:19:51.773 I/dzt_test(26317): at android.app.ActivityThread.main(ActivityThread.java:5333)
04-09 14:19:51.773 I/dzt_test(26317): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 14:19:51.773 I/dzt_test(26317): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 14:19:51.773 I/dzt_test(26317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
04-09 14:19:51.773 I/dzt_test(26317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
04-09 14:19:51.773 I/dzt_test(26317): at dalvik.system.NativeStart.main(Native Method)
5、
// 主动抛出异常调试
try {
Log.i(TAG,
"--------------------------------NullPointerException-----------1");
throw new NullPointerException();
} catch (NullPointerException e1) {
// TODO: handle exception
Log.i(TAG, "--------------------------------NullPointerException");
Log.e(TAG, Log.getStackTraceString(e1));
// e1.printStackTrace();
}
Log.i(TAG,
"--------------------------------NullPointerException-----------end");
04-09 14:19:51.773 I/dzt_test(26317): --------------------------------NullPointerException-----------1
04-09 14:19:51.773 I/dzt_test(26317): --------------------------------NullPointerException
04-09 14:19:51.774 E/dzt_test(26317): java.lang.NullPointerException
04-09 14:19:51.774 E/dzt_test(26317): at com.dzt.testapp.MainActivity.ThrowException(MainActivity.java:88)
04-09 14:19:51.774 E/dzt_test(26317): at com.dzt.testapp.MainActivity.onCreate(MainActivity.java:63)
04-09 14:19:51.774 E/dzt_test(26317): at android.app.Activity.performCreate(Activity.java:5343)
04-09 14:19:51.774 E/dzt_test(26317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-09 14:19:51.774 E/dzt_test(26317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
04-09 14:19:51.774 E/dzt_test(26317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
04-09 14:19:51.774 E/dzt_test(26317): at android.app.ActivityThread.access$800(ActivityThread.java:151)
04-09 14:19:51.774 E/dzt_test(26317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
04-09 14:19:51.774 E/dzt_test(26317): at android.os.Handler.dispatchMessage(Handler.java:110)
04-09 14:19:51.774 E/dzt_test(26317): at android.os.Looper.loop(Looper.java:193)
04-09 14:19:51.774 E/dzt_test(26317): at android.app.ActivityThread.main(ActivityThread.java:5333)
04-09 14:19:51.774 E/dzt_test(26317): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 14:19:51.774 E/dzt_test(26317): at java.lang.reflect.Method.invoke(Method.java:515)
04-09 14:19:51.774 E/dzt_test(26317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:830)
04-09 14:19:51.774 E/dzt_test(26317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
04-09 14:19:51.774 E/dzt_test(26317): at dalvik.system.NativeStart.main(Native Method)
04-09 14:19:51.775 I/dzt_test(26317): --------------------------------NullPointerException-----------end
虽然以上方法不尽一样,但打印出来的堆栈是一样的。