Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 學習筆記:查看與輸出日誌資訊

編輯:關於 Android 程式設計

查看日誌與錯誤資訊

由於 Android 不會在 Console 控制台直接輸出錯誤資訊,因此我們需要透過 Eclipse 的選單開啟 LogCat 檢視:Window → Show View → LogCat。

如何輸出日誌資訊

Android 提供 android.util.Log 類別來輸出日誌,常見用法如下:

package com.credream.test;
import android.util.Log;
public class LogTest {
  private static final String TAG = "LogTest";
  public void testOutLog() throws Throwable {
    Log.i(TAG, "資訊內容");
  }
  public void testOutLog2() throws Throwable {
    System.out.println("標準輸出");
  }
  public void testOutLog3() throws Throwable {
    System.err.println("錯誤輸出");
  }
}

執行與觀察

選擇要測試的方法,右鍵選擇「Run As → Android JUnit Test」即可執行。其中:

System.out.println(...) 的 tag 為 System.out;
System.err.println(...) 的 tag 為 System.err;
Log.i(TAG, ...) 則使用自訂的 TAG。

值得一提的是,Log.i 輸出中文時可能出現亂碼,這是因為部分環境不支援中文編碼,但程式本身並沒有問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved