開發Android程式時,Android程式利用LogCat進行Debug Log的顯示。

首先要開啟LogCat,位置為Eclipse -> Window -> Show View -> Other... -> LogCat -> OK

device036  

device037  

接著就會出現這個視窗

device038  

Log 所使用的方法為下列五個: 

Log.v   Verbose 詳細訊息皆輸出
Log.d   Debug 除錯訊息輸出
Log.i   Information 一般的通知訊息
Log.w   Warn 警告訊息提示
Log.e   Error 錯誤訊息提示

 按下不同模式按鈕會顯示的訊息種類不同,例如按下E只會顯示Log.e內容,按下W則會顯示Log.e及Log.w內容

device039  

  模式   顯示的訊息種類         
  Error   Log.e
  Warn   Log.e Log.w
  Information   Log.e Log.w Log.i 
  Debug

  Log.e Log.w Log.i Log.d

  Verbose   Log.e Log.w Log.i Log.d Log.v

 小試身手小範例

Java檔

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class LogExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Log.e("Log Activity Test", "這是 Log.e!!");
Log.w("Log Activity Test", "這是 Log.w!!");
Log.i("Log Activity Test", "這是 Log.i!!");
}
}

XML檔

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Log測試"
android:textSize="35sp"
android:layout_margin="5sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請於LogCat查看結果 : )"
android:textSize="20sp"
android:layout_margin="5sp"
/>
</LinearLayout>

device039  

Information模式

device040  

Warn模式

device041  

arrow
arrow

    S 發表在 痞客邦 留言(0) 人氣()