Android Uncategorized

Debugging errors in Android

Android has a great framework to work in. However, it is in its infancy and debugging my code has been a very big problem for me. Whenever something goes wrong in the code, I always get “source not found” error. It doesn’t tell me I have a nullPointer exception , or string is incomplete.  No matter what’s wrong with the code, I always get “source not found” error. The reason that happens is Android doesn’t pack the source along with the application it sends to the emulator (maybe they should do that for a debugging run). After searching the net for a while, I came across this solution – wrap your code in a try and catch block and then log the exception to console. Here is an example :

public class SomeClass extends ListActivity {
	private AlertDB db ;
	private static final int ALERT_ADD = 100;

	public void onCreate(Bundle savedInstanceState) {
	try {
		db = new AlertDB(this);
		Cursor c = db.fetchAllAlerts();
	} catch (Exception e) {
		Log.e("ERROR", "ERROR IN CODE:"+e.toString());

Doing that will display a neat little line in your logcat window (look at the screenshots at the bottom). And, instead of getting the annoying “source not found” error. You will know what the actual error is. In Android, you can log three types of events – information, warning and error. Information is just verbose text being written out to the log.  I don’t need to explain warning and errors.

For logging information you can use :

Log.i(TAG, message);

For logging warnings you can use :

Log.w(TAG, message);

I have already shown how to log errors in the code above.

Click to enlarge
Click to enlarge
Click to enlarge
Click to enlarge

By Moazzam

Moazzam is a software developer working in the big apple. He has over a decade of experience in development. While most of his work involves developing web applications, he does development for Android and Windows Mobile in his spare time.

2 replies on “Debugging errors in Android”

Leave a Reply

Your email address will not be published. Required fields are marked *