Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 打印堆棧的幾種方法

Android 打印堆棧的幾種方法

編輯:關於Android編程

在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
雖然以上方法不盡一樣,但打印出來的堆棧是一樣的。


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved