So in a couple of movies now I've showed you how to use DDMS to actually view
log information that's coming from your device, or from the emulator, and this
can help you to debug errors that are occurring and also just to look at what's
going on on your device.
Now we are going to get into some more traditional-type debugging and how you go
about debugging your Android applications.
So I've created just a blank project here.
I've called it Debugging.
And we are just going to go into the layout.
I am just going to add a few things in here. So we have our TextView.
I am going to bring in a button.
And then I'll just bring in an EditText field, just so we have some stuff in there.
I'll save that.
Now if we want to be able to debug our application, we need to add something to
our Android manifest file that allows it to do that.
So I am going to open up the manifest and then go to the Application tag.
We can see at the top-right we have this Debuggable property.
We need to set that to true if we want to be able to connect our application to
the debugger here in Eclipse.
Okay, so we have that done.
Now let's go into our Java code, and we are just going to respond to the click
event on that button.
I will create a new instance of Button,
findViewById. There is my button.
Of course, I'll cast it to a button.
Now we're going to set the OnClickListener, and just going to do an anonymous
event handler here and of course let me actually import OnClickListener.
Again, I showed you DDMS, and more specifically, LogCat.
Here we can see LogCat, and again this is logging everything that's actually
happening on our Android device.
So every time, if I hit the Home screen, if I do anything on this device, it's
actually going to be logging certain information. But I can put my own
information in there from my application.
And let's go back to our Java code and to do that, we are going to use the Log
class, and this is in the android.util package.
Now there is bunch of different methods in here that I can use to log stuff to LogCat.
I can choose the d method, which is going to send a debug message, where it passes
in a tag and a message.
I can even actually send a Throwable object if I want to
just log my exception.
We have the e method, which actually logs an exception;
i logs an info message;
v, a verbose message; w actually is a warning; and the funny one down here is
wtf, which is actually What a Terrible Failure, and this is something that's not
supposed to happen on the system.
It's pretty dramatic.
We are just going to use d.
Now for the tag, this can be anything, just so that I can recognize it in LogCat.
So I am just going to do uppercase LEE, and then I'll just say, "thebutton was clicked."
So this is going to log it, but what I want to do is actually I want to set
a break point here so that it will pause my application and allow me to debug my application.
So I am just going to go into the left-hand column. I'm sure you've done this if
you debugged any code before.
I'm just going to double-click it to add a break point, and then I'm going to hit
this little bug up here to debug.
And we'll want to run it as an Android application, and now let's go over to the emulator.
So when this launches up, it's going to show me my user interface.
Now you can see we get a message. It was waiting for the debugger.
Basically, it was waiting for the debugger in Eclipse to connect itself to my application.
So now they are connected.
So now when I click this button, you can see it's automatically brought me to the
Debug perspective here in Eclipse.
Now depending on what you've done before with Eclipse it may pop up a
message saying, "Hey!
Do you want to go to the Debug perspective?" Just click Yes.
But since I am here, now I have all the traditional debug tools that I am used to.
So I can actually go through the different variables in my application.
I can set values for those variables.
I can look at my break points. I can see the code where it's actually stopped at
that break point. And I have LogCat down here.
And one of the things when I maximize this, you'll notice that it hasn't
actually logged out that message, and that's because we're still paused on that break point.
So if I actually click Resume here, so we go back to LogCat, we should now see
the message, and we could see it down here, thebutton was clicked.
This tag is basically just identifying which process, or which application, is
sending this message.
Now one of the things you'll notice about LogCat is it logs a tremendous amount
of information from everything that's happening on the device.
Well, what if I only want to see the information coming from my app?
Well, I can set up a filter here.
Click on this Plus sign.
I'll just call the filter name Lee.
By Log Tag, so if I just want to filter out the messages with the tag of Lee, I
can click OK, and now you can see anything that I've logged with Lee is
actually shown here.
So that's how I log information to LogCat, and now you can see we are still on
this breakpoint and again,
I can do all that the traditional debug-type activities here.
So this is great for traditional debugging of my code, but what about the user interface?
So there's actually a great tool included with the Android SDK that allows you
to debug your layouts and your UI.
So I am just going to disconnect from the debugger here, and I am going to go to
my android-sdk folder, which is in C/Program Files/Android/android-sdk, and then
go into the tools folder.
So the tool is actually called hierarchyviewer, and this is a batch file
on Windows, so I can just double-click it; on Mac you've to launch it from the Command line.
And what this tool does is it shows me all of the applications or processes that
are actually running on my device, and in this case it's the emulator.
And here is the application that I want to debug, and I am going to click
Load View Hierarchy.
And what this is doing is it's going through and inspecting the user
interface of my application.
And now it's displaying it in this tree view.
Now here I can actually drag things around.
I can use my mouse wheel to zoom in and out.
This is basically showing me the structure of my application from a UI perspective.
Now you probably wonder, what are these three colored circles?
Well, this represents the performance, or how fast this thing has actually
rendered. And now there are three steps to that:
there is the measure step, where it measures how much space it's going to need;
then the layout step, where it actually lays out things; and then finally the
draw step, where it actually paints it to the screen.
And obviously, if you see all green, that's good. And this is just relative to
other views in your application.
But if you had yellow or of course red is going to be the worst,
that will then allow you to kind of pinpoint, these are the areas that I need to kind of watch.
And in general, and when you're creating Android applications, you want to keep
your View Hierarchy as flat as possible.
You don't want tons and tons of nested layouts, but I also come through,
I can click this, and I can see in this wireframe view where this actual Button
control is laid out.
I can also go through all of these properties to find out detailed information
about where it's laid out, the size, all these different properties.
I can also double-click this to see kind of a live preview, and we could
see what it looks like on a white background, black, and I can also get some extra information.
So I definitely recommend you come in here to look at your layout hierarchy,
because this can be a really important place to improve the performance of your application.
So there are other tools as well. So this is, I am on the Dev Guide on the SDK
site, in the Debugging section.
And if you want to do really detailed performance tracing of your application,
there is a tool called TraceView.
Now this is more of an advanced tool, so we are not going into it in this
course, but it gives you this Timeline view so you can see over time where the
bottlenecks in your code are, and you do this by adding tracing information in your code.
But this can be a really good way, again if you're experiencing real problems
with your code or if you're creating an application where performance is of the
upmost priority, TraceView is definitely a tool that will help you to do that.
So that's the basics of being able to debug your application here in a
traditional way inside of Eclipse, but then using some of the other tools in the