Debugging in Android cannot be done using System.out.println(...) because as we know Android is not running on a normal VM but inside of emulated hardware on the DalvikVM. ( To be honest, it can be done but is should definitely be not your choice)

But don't worry, Android provides a much more powerful debugging feature - The LogCat.

The LogCat

The LogCat is a part of the DDMS (Dalvik Debug Monitor service) that provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in the LogCat, which then can be viewed and filtered.

If you cannot see A, do it the B way.

If you cannot see A, do it the B way.

Picture 15 Opening the DDMS View

Using the LogCat

Every Android log message has a tag and a priority associated with it.

The tag of a log message is a short string indicating the system component from which the message originates (for example, "View" for the view system).

The priority is one of the following character values, ordered from lowest to highest priority:

As we are using Eclipse we can simply filter by priority clicking the "V-D-I-W-E" Buttons in the LogCat View one can also see below. You'll love the ADT plugin for that feature, because, in the masses of output the whole system generates, it is really hard to find anything without filters.

Picture 16 The LogCat

The usage in the code is pretty simple. You need to make a single import:

import android.util.Log;


Then you are able to use debugging statements, like this:

Log.d("DEBUGTAG", "My debug-message.");

When some critical things happened you can pass the Throwable/Exception to Log.e(...) ,to get the StackTrace printed to the LogCat:

try {

throw new Exception();

}catch(Exception e){

Log.e("DEBUGTAG","Error occured"


0 0

Post a comment