As a beginner we always get lost if application is crashed and if we don’t know how to debug the android applications using logcat. This tip will be helpful for developer who are new to Android Application development.

What is Logcat?
LogCat is integrated into DDMS (Dalvik Debug Monitor Server), and outputs the messages that you print out using the Log class along with other system messages such as stack traces when exceptions are thrown. View the topic for more information on how to log messages to the LogCat.

Step 1: Writing Log Messages: The Log class

If you already have an application for which you want to print messages on logcat, you can use Log class. It has following methods

  • v(String tag, String msg) (verbose)
  • d(String tag, String msg) (debug)
  • i(String tag, String msg) (information)
  • w(String tag, String msg) (warning)
  • e(String tag, String msg) (error)

Here first parameter is tag which is used to filter the log messages and second parameter is printed on logcat. Following is the sample:

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

Output on logcat will look like below

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

Step 2: Debug the Application

Right click on the project and click Debug As => Android Application.

Step 3: Open your console window and keep checking if your emulator is connected

As soon as you debug the application, open the console window and keep checking if emulator is connected. If emulator get disconnected, it will show like below in Console screen of the eclipse IDE.

If it shows disconnected like

in above screen, then it means emulator is not connected to ADB and you cannot get log messages. So close the emulator and again debug the application. If its not shown then you are ready to get log messages :-).

Step 4: Starting Logcat

Two ways to view the logcat msgs. First from command prompt and other from Eclipse.

1) From command prompt
Just change the directory to [android-sdk-path]/platform-tools and run the following command

adb logcat

It will show all log messages printed using Log and System.out.print methods.

2) From Eclipse

Navigate to Window => Show View => Other => Android => Logcat to view logcat messages in eclipse.

Step 5: Viewing Stacktrace if crashed

If Application is crashed, it shows the dialog like below:

So your application is crashed now and you need to find in which class and on which line the crash is. Logcat will also print the stacktrace for you which will help you to detect the crash. Stacktrace looks like below:

11-11 01:58:47.472: WARN/dalvikvm(254): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-11 01:58:47.492: ERROR/AndroidRuntime(254): Uncaught handler: thread main exiting due to uncaught exception
11-11 01:58:47.533: ERROR/AndroidRuntime(254): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobisys.android.vocadroid/com.mobisys.android.vocadroid.search.SearchActivityList}: java.lang.StringIndexOutOfBoundsException
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.os.Looper.loop(Looper.java:123)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at java.lang.reflect.Method.invokeNative(Native Method)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at java.lang.reflect.Method.invoke(Method.java:521)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at dalvik.system.NativeStart.main(Native Method)
11-11 01:58:47.533: ERROR/AndroidRuntime(254): Caused by: java.lang.StringIndexOutOfBoundsException
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at com.mobisys.android.vocadroid.HelperApp.fillSearchWordlist(HelperApp.java:253)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at com.mobisys.android.vocadroid.search.SearchActivityList.addListView(SearchActivityList.java:156)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at com.mobisys.android.vocadroid.search.SearchActivityList.onCreate(SearchActivityList.java:111)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-11 01:58:47.533: ERROR/AndroidRuntime(254):     ... 11 more
11-11 01:58:47.582: INFO/Process(61): Sending signal. PID: 254 SIG: 3

Hurray, we can see from line 17 that the crash is at HelperApp class at line 253 and exception thrown is StringIndexOutOfBoundException i.e. we are accessing wrong index of string.


To detect the logical errors and crash in android application, you can use the Logcat from command prompt as well as from Eclipse.

AUTHOR: Mahavir Jain

Founder @CodeToArt, Leads Android Development at CodeToArt.

No Comments

Leave a Comment

Your email address will not be published.