Use Ddms and add debug message in your application.

DDMS是開發Android所必備的debug tool,它的功能有:

–Devices - Shows the list of devices and AVDs that are connected to ADB.
–Emulator Control - Lets you carry out device functions.
LogCat - Lets you view system log messages in real time.
Threads - Shows currently running threads within a VM.
–Heap - Shows heap usage for a VM.
–Allocation Tracker - Shows the memory allocation of objects.
File Explorer - Lets you explore the device's file system.

紅色是最重要的功能.


[ Using LogCat ]

LogCat is integrated into DDMS, and outputs the messages that you print out using the Log class. 
因為我們要debug程式,所以要了解其用法:
•logcat messages type:
– Verbose
– Debug
– Info
– Warn
– Error
The Log class:
v(String, String) (verbose) 
d(String, String) (debug) 
i(String, String) (information) 
w(String, String) (warning) 


For example: Log.i("MyActivity", "MyClass.getView() — get item number " + position);

output : I/MyActivity( 1557): MyClass.getView() — get item number 1;

知道語法後就可以開始debug了.

[ 改寫程式,加入debug message ]


 package com.firstapp;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class firstapp extends Activity {
    
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          TextView tv = new TextView(this);
          tv.setText("Hello, Mark's Android first APP");
          int position=1;
          Log.i("first app","this app- first app.java" + position);
          setContentView(tv);
         
      }            
}


[ Output debug message with logcat]

留言