Android中列印代碼的調用層次

來源:互聯網
上載者:User
http://hubingforever.blog.163.com/blog/static/171040579201172282210766/在我們調試Android代碼的時候,有些時候需要取得當前代碼的調用層次(調用棧)。本人現知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("robin", e);        }注意我們其實可以直接構建Exeption或Throwable。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.