Android 打印堆栈的几种方法

下图转自:https://blog.51cto.com/zhaoyanjun/3814556

 

 

 

android.os.Debug
Debug类里面有几个getCaller方法可以获得当前的StackTrace,在需要调试的方法法中直接调用即可

public void dumpStackTrace(){
  android.os.Debug.getCallers(9);
}

Log.d(TAG,"log "+Debug.getCallers(9));

 

Android 打印堆栈的几种方法

public void ThrowException() {
    Thread.dumpStack();


    // 调试打印堆栈而不退出
    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");
}



posted @   行走的思想  阅读(2835)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2018-07-11 Filezilla Download for Linux (deb, rpm, txz, xz, amd64, i386, i486, i586, i686, x86_64)
2017-07-11 ubuntu14.04 LTS Visual Studio Code 编辑器推荐
点击右上角即可分享
微信分享提示