在解bug或者排查问题时,经常需要知道一个方法在运行到里面的时候,是由谁来调用的,调用路径是什么(调用堆栈)
Android 原生 SDK提供了方法:
//获取调用堆栈的方法
String stackTraceString = Log.getStackTraceString(new Throwable());
此时stackTraceString
内容是:
java.lang.Throwable
at com.example.example.Test.<init>(Test.java:51)
at com.example.example.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:8066)
at android.app.Activity.performCreate(Activity.java:8054)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1313)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3733)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3939)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8147)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)