Friday, June 24, 2011

Met with a CRASH , What to do next ??

A Crash is something that deters developer's pace. Trust me, to analyze the android app crash is a piece of cake with android's adb. It's just that we need to be familiar with certain internal stuff. I'm mentioning few of the things that i came across these days that might be handy for a new android developer. This is will be like a check list that u need to make sure before stepping through the code.

First and foremost thing is "LogCat", its already there in your eclipse IDE. If you are not able to see the logcat window, then go to Window -> show View -> Other -> Android -> Log Cat. This is an important tool, which catches all the application logs, exceptions, debug statements and of course the verbose. If you have set this up, then i'm sure you have almost fixed your crash ;)

Second, Now you can scroll and see all the red coloured horrible lines of your code's exception message inside the LogCat. Please do not start with the last line of the exception thrown, I'm sure you will not have the slightest understanding of what has went wrong. Scroll to the top and read the function which is at the top of call stack (similar to ur windbg call stack). You may get a clue - which function has the problem.

Third, Most often you might end up with an error of missing activity or unable to start the activity. You would created the xml file, and u might used the proper id in the java file. But stil, most of the time we might forget to add them to the androidManifest.xml file. If you have not added it here, AOS does not identify your new activity. So make sure all the activities you have used are mentioned here. This includes all the broadcast receivers, services etc.,

Fourth one also involves the androidManifest.xml file. This time, you need to use this line :
e.g., uses-permission android:name="android.permission.CALL_PHONE" in ur manifest file which asks the permission to the user. If you have not mentioned something like this, and if you programmatically try to do those operations, again its a crash !
Examples of uses permission:
uses-permission android:name="android.permission.READ_SMS"
uses-permission android:name="android.permission.RECEIVE_SMS"
uses-permission android:name="android.permission.READ_CONTACTS"
uses-permission android:name="android.permission.READ_PHONE_STATE"
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
uses-permission android:name="android.permission.MODIFY_PHONE_STATE"
uses-permission android:name="android.permission.SEND_SMS"
uses-permission android:name="android.permission.WRITE_SMS"
make sure to use the appropriate permission in ur manifest, not all are required !

Fifth, its the most familiar problem "java-NullPointer-exception".. This problem can be caused in a variety of ways and i'm sure i have caused this issue in almost all possible ways :P Anyways, most common issues are caused when we try to access the variable or the object without initializing it.
For e.g, say you have a text view with an id = textview01 and if you need to access it in your java file.
You must : mTextView = (EditText) findViewById( only after this you can do this -> String str = mTextView.getText().toString();
There are other possibilities like string array initialization, cursor out of bounds, array index out of bounds, etc. which you must be very familiar. Please take spl care when you are accessing a cursor adapter to query values.

Sixth is, try to use "try catch" where you do string operations, db queries, web service communications,threaded functions,content resolver operations and array parsing etc., Try catching can avoid that terrible "sorry -- close" dialog. (Which i know is terrible to look at ;))

No comments:

Post a Comment