views:

86

answers:

3

What is the best way to debug an android application? Every once in a while the application crashes, an it says application has stopped unexpectedly, it never says why it crashes, or where in my code, what's the bet way to find the reason for crashes, and exceptions?

+2  A: 

If you're using ADB just launch your app in debugging mode to gain a possibility to watch variables/expressions at runtime. You also can see the stacktrace in a Logcat window of your IDE if your app crashes.

Aleksander O
+2  A: 

there are several ways to do that, activate the LogCat and you will see there detailed info about what happens with your App.

or you will implement an error Handling sending the Exception info to a Toast

 try {
    ...
    your code
    ...
 } catch (IOException e) { 
     Toast.makeText(getApplicationContext(), "Exception Info " +     e.getCause()),Toast.LENGTH_LONG).show();
    e.printStackTrace();
 }      

updated

Jorgesys
This is bad. You should really let the exception crash the application if you're not going to handle it in some meaningful way.
ognian
hi ognian , I think sometimes is better show the Error message to the users than show the ugly message "application has stopped unexpectedly" and force to close the entire app =(
Jorgesys
It's bad practice to display the full e.getMessage() to the user though. Dialog should explain what went wrong, not give a stack trace. Plus, there are security issues with this approach. You don't know what's in that message. I've had my SQL queries and database schema exposed this way :P
CodeFusionMobile
hehe Ok i understand the approach, even more if the toast just show the info few seconds :P, and this never can't be in a release version.
Jorgesys
A: 

Use the Android's Log class, and logcat, for example:

Log.d( "Your app name", "The value of x is: " + x );

And in logging output you'll get something like:

D/Your app name( 1001): The value of x is: 96

You can get logging output from command line by running: "adb logcat", or using Dalvik debug monitor (ddms in the tools directory of Android SDK)

Using Log is superior to using Toasts since Toasts are gone quite quickly, and logcat will show you a lot of information upon application crash (like filename and line number which caused unhandled exception)

Hidden Android