Several methods of the Android print stack

Source: Internet
Author: User
Tags throwable

In the Android debugging process often appear the results of the program is not our expected results, it is necessary to add log printing debugging, see whether the call process is correct, at this time need to print the program's call stack, especially the Android code is quite large, the printing stack is more conducive to our analysis of the problem, The following is a record of the usual use to not exit the program print stack method.

The validation method is simple, create a method in the Activity class ThrowException, call this method in OnCreate, see what is printed

public void ThrowException () {//debug print stack without exiting log.d (TAG, log.getstacktracestring (New Throwable ()));//Create exception print stack exception e = new Exception ("This is a log"); E.printstacktrace ();//Gets the stack for the current thread for (Stacktraceelement i:thread.currentthread (). Getstacktrace ()) {log.i (TAG, i.ToString ());} RuntimeException re = new runtimeexception (); Re.fillinstacktrace (); LOG.I (Tag, "StackTrace", re);//actively throw exception debug try {log.i (tag, "-------------------------------- NullPointerException-----------1 "); throw new NullPointerException ();} catch (NullPointerException E1) {//Todo:handle exceptionlog.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.throwable04-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 log04-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.

Gets the stack for the current thread 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): stacktrace04-09 14:19:51.773 i/dzt_test (26317): java.lang.runtimeexception04-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.

Aggressive Throw Exception Debug try {log.i (TAG, "--------------------------------nullpointerexception-----------1"); throw new NullPointerException ();} catch (NullPointerException E1) {//Todo:handle exceptionlog.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-----------104-09 14:19:51.773 i/dzt_test (26317):--------------------------------nullpointerexception04-09 14:19:51.774 e/dzt_test ( 26317): java.lang.nullpointerexception04-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
Although the above method is different, the printed stack is the same.


Several methods of the Android print stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.