Debugging an Android App
Before starting that how to debug an android application, it is necessary to know that what is “Debugging”?
“Debugging is a methodical process of finding and reducing the number of bugs and defects in a computer program”
Advantage of Debugging:
“Debugging is a methodical process of finding and reducing the number of bugs and defects in a computer program”
Advantage of Debugging:
When you start to make any application in android or in any programming language you will face some bugs, for solving them you need a good understanding of debugging your application.
There are following ways to debug an application in android:
1.) Logcat
Android provides a general-purpose logging package that you can take advantage of to log informational or error messages from your running application. Perhaps of more importance, Android uses this facility extensively to tell you what is going on as it starts up, initiates your application, and tries to run it
1.) Logcat
Android provides a general-purpose logging package that you can take advantage of to log informational or error messages from your running application. Perhaps of more importance, Android uses this facility extensively to tell you what is going on as it starts up, initiates your application, and tries to run it
2.) Eclipse Debugger
Eclipse provides a source-level debugger that the Android SDK connects with the running Dalvik bytecode, so you have all the debug capability you’d normally expect from a Java program running under Eclipse.
Eclipse provides a source-level debugger that the Android SDK connects with the running Dalvik bytecode, so you have all the debug capability you’d normally expect from a Java program running under Eclipse.
3.) Android Debug Bridge (adb)
This provides a command-line debugging interface to a running Android phone or emulator.
This provides a command-line debugging interface to a running Android phone or emulator.
4.) DDMS
Android also provides a special window-oriented debugging environment custom tailored for Android and the Dalvik VM. You can find more about DDMS in our next article “What is DDMS and how it works”
5.) Traceview
An Android-specific utility that tracks all the method calls your application executed and the time spent in each method.
Android also provides a special window-oriented debugging environment custom tailored for Android and the Dalvik VM. You can find more about DDMS in our next article “What is DDMS and how it works”
5.) Traceview
An Android-specific utility that tracks all the method calls your application executed and the time spent in each method.
In this article, we will discuss the topic that how to use Logcat and Eclipse Debugger.
1.) Logcat:
First we will understand Logcat; this is most easy way to get error messages from your running application.
To open the Logcat window: go to
Window > Show view > Other, Android > Logcat.
Window > Show view > Other, Android > Logcat.
You should be able to see Logcat at the bottom of your screen just like this:
As you can see above, Logcat has different levels of logging.
V stands for Verbose (lowest priority)
D stands for Debug
I stand for Info
W stands for Warning
E stands for Error
D stands for Debug
I stand for Info
W stands for Warning
E stands for Error
How to use??
To use Logcat you have to import android.util.Log in your project.
Then you can call the static class Log.
Here is a sample code snippet:
To use Logcat you have to import android.util.Log in your project.
Then you can call the static class Log.
Here is a sample code snippet:
package com.app.SampleProj;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends Activity {
private static final String TAG = "MyActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG,"On Create");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG,"On Destroy");
}
}
private static final String TAG = "MyActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG,"On Create");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG,"On Destroy");
}
}
Here is the Logcat:
This is How Logcat Works…!!
You can also filter the Log for making it more clean:
Click in the “+” sign of your Logcat window at the right side of screen, and add new Filter with tag name:
You will see a new Tab which will contain only specific Tags…
This is all about the Logcat, now we come to our next topic Eclipse Debugger.
2.) Eclipse Debugger:
Eclipse provides a built-in “Debugger”, in which you can set “breakpoints” where the program will pause, and allow you to step through the program line by line and inspect the values of variables, amongst other features.
To open the Debugger: go to
Window->Open Perspective->-Debug
Window->Open Perspective->-Debug
You will get a debug Perspective like this:
Now you need to set breakpoint to use debugger. You can set breakpoint by placing your cursor on the marker bar (along the left edge of the editor area) on the line with the suspected code. Double-click to set the breakpoint or right click and click Toggle Breakpoint:
Now debug the application by right click on the selected project and click on the debug as ->android application
Or
By click on the bug button in the tool bar:
Or
By click on the bug button in the tool bar:
The following figure shows the execution break at the first line in the onCreate() method of the activity. You can Step Into (), Step Over (), Step Return (), etc.
Step Into () or F5 key: The next expression on the currently-selected line to be executed is invoked, and execution suspends at the next executable line in the method that is invoked.
Step Over () or F6 key: The currently-selected line is executed and suspends on the next executable line.
Step Return () orF7 key: Execution resumes until the next return statement in the current method is executed, and execution suspends on the next executable line.
This is all about debugging .Hope this article will help you!!
1 comment:
thanks for more information abt debugging.i need how to store multi selected OnClick value from the simple listview.how to get the value like getItemAtPosition. plz reply (albinrath@gmail.com)
Post a Comment