August 30, 2010

Android Basics –1 (LogCat)


When an App is run as Android Application the whole project will run on emulator as .apk file. in this apk file all the files, images, xml, java byte code will be packed. When a new Android Project is created a lot of files and folders are created.Res folder contains all the resources for Layout, Strings, images as Drawable which can be used by any part of the application. Layout folder contains all the XML files which describes the layout. String folder contains all the string resources which can be used in the app.However I want to say something which is beyond what books say.


Whenever there is a runtime error the app in the Emulator just closes mostly saying “Force Close”. User cannot do anything in this situation except accept the error and click on “Force Close”. This sort of error occur manytimes in development of any app and Developers starts scratching their heads to find out what could be wrong in the code. In this situation I recommend to watch out Logcat. This is the tool which displays what's happening in the Emulator and whenever there is an an Error, Logcat clearly specifies the error by telling there was a “NullPointerExcepion” while accessing a particular function or “ArrayOutOfBoundException” at a particular line etc..


To access logcat u have to change the perspective from java to DDMS, then look a mid portion of the screen where it looks similar to the given image below


In this Logcat its easy to find any errors/ exceptions at runtime and even We can use this logcat as output rather than getting output in Emulator screen. To use logcat as output a funtion must be run in Java code and this function is

//Log.i(String tag, String message);done as following
Log.i("Hi"."Button Clicked");

Here when this line is run it displayed “Hi” under tab and “Button Clicked”  as message.

Hope U liked this basic understanding and please feel free to ask any doubts..

No comments:

Post a Comment