Android中打印代码的调用层次
查看次数47961 发表时间2012-11-18 22:57:53
在我们调试Android代码的时候,有些时候需要取得当前代码的调用层次(调用栈)。
(转发请注明转自:学PHP)
本人现知3种方法,一种是java传统的方法,另一种是Android提供的方法,还有第三种需借助于Exeption或Throwable。
第一种,通过getStackTrace()
这种是java的传统方法,即在你的代码处,通过Thread.currentThread().getStackTrace()得到一个StackTraceElement数组,然后打印该数组。
示例1:
StackTraceElement st[]= Thread.currentThread().getStackTrace();
for(int i=0;i<st.length;i++)
System.out.println(i+":"+st[i]);
这样你就把此处代码此时的调用层次(调用栈)打印出来了。
第二种,使用android.util.Log.wtf()
我们可以直接使用android提供的android.util.Log.wtf()系列函数来输出一个日志.在输出日志的同时,它会把此处代码此时的执行路径(调用栈)打印出来。
示例2:
Log.wtf(tag, "this a fake erro");
第三种,借助Exception
示例3:
try{
cm.setPrimaryClip(ClipData.newRawUri(null, Uri.parse(text.toString())));
}catch(Exception e)
{
e.printStackTrace();
}
示例4:
try{
................
}catch(Exception e)
{
Log.wtf("r
(转发请注明转自:学PHP)
相关推荐
- Android手机中紧急号码的定制 (2012-11-18 22:57:56)
- Android之高仿记事本、备忘录、便签 (2012-11-18 22:58:03)
- android与javascript交互调用 (2012-11-18 22:58:22)
- 搭建XMPP协议,实现自主推送消息到手机 (2012-11-18 23:01:23)
- android 窗口小部件 的实现 (2012-11-24 00:31:59)
- Android中判断是否有声音在播放 (2012-11-18 22:57:48)
- Android之网络学习资源汇集(更新) (2012-11-13 23:26:58)
- Android应用开发学习笔记(1)开发环境部署 (2012-11-12 13:10:05)
- android如何运行自带demo (2012-11-12 13:09:59)
- android游戏开发---03---SQL数据库 (2012-10-05 23:48:55)
- 1楼 Agatha说: 2016-12-22 04:13:21
-
Have you even seen my videos and ratio, i can dig you out myself. Just give me the bump!!! Channel- dubebestlin, I'll be talking to you again, i am sure of that!
- 2楼 Grizzly说: 2016-12-22 08:51:26
-
Thank you so much for sharing your sacred and precious truth. I see a book here someday…a book that could really help people who feel the exact same way and have never been able to put it into wos8;&#d211rsuch raw & eloquent and honest words.xo,j
发表评论