編輯:關於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雖然以上方法不盡一樣,但打印出來的堆棧是一樣的。
本文開發一個基於Service的音樂播放器,音樂由後台運行的Service負責播放,當後台的播放狀態發生變化時,程序將會通過發送廣播通知前台Activity更新界面;當點
一、前言前一段時間在做視頻開發,由於本人剛接觸視頻開發這塊,所以領導沒有對我提很高的要求,僅僅要求能夠播放本地視頻即可。我想怎麼簡單怎麼做。於是選擇用Android Vi
前言安卓開發中,在寫布局代碼的時候,ide可以看到布局的預覽效果。但是有些效果則必須在運行之後才能看見,比如這種情況:TextView在xml中沒有設置任何字符,而是在a
Android中Service的一個Demo例子Service組件是Android系統重要的一部分,網上看了代碼,很簡單,但要想熟練使用還是需要Coding。本文,主要貼