Advertisement
English transcript
A crash in an app is when it unexpectedly quits and
shows a dialogue message saying that the app has just stopped.
It happens to Android developers many, many times a day.
Even the most experienced ones.
Sometimes the problem is due to a typo in their code, or a silly mistake.
Other times it can be due to a fundamental misunderstanding
of how to write the code for a certain feature.
While your reaction may be to panic, the voice of reason inside your head
should tell you yo go ahead and try to debug the app.
In this video we're going to learn how to debug Android app crashes.
To debug means to identify and fix bugs or errors in your app.
If it's a really obvious error like a java syntax error,
then Android's DU can catch that for you and
show a red error before it tries to install the app on your phone.
This is called the compile time error because it will fail to create any app
to even install on your device.
If you want to try creating a compile time error, go ahead and just delete
this parentheses or this semicolon and then you'll see the error pop up.
This is like in lesson one when we tried to create an XML error intentionally.
And then we read the error message and then we undid the change.
Other potential errors are if I try to delete this opening brace, then I'll get
an error, or if I forget this closing brace, then I also get an error.
But sometimes the error only happens when you start using the app.
This is called a run time error because it happens when your app is running.
Let's intentionally create a crash in the app and
figure out the steps of how to solve it, so that when you have a real crash,
you'll know how to tackle it on your own.
This is actually based off of a real crash that I saw a student encounter.
So it's very likely to happen to beginners.
I'll first walk you through all the steps and
then you'll have a chance to do it yourself.
To intentionally create a crash in the app,
I'm going to modify the MainActivity Java file.
I'll change the name of this method to be submitOrders with a s.
Then I'm going to run it on the device.
Everything looks fine at first, but
then when I click on the ORDER button, it crashes.
It gives me the message, unfortunately, the app named, Just Java has stopped.
And the only option that I have is to hit OK to exit the app.
Fortunately, we can use Android Studio to figure out what actually happened.
When you run the app on your device,
you may see this Android DDMS window pop up at the bottom of the screen.
This show you information to monitor your Android device.
All the stuff that goes on behind the scenes that users don't see.
If it's not already open, you can click on this tab down here that says Android,
and that should open up Android DDMS for you.
You can adjust the height of this window by dragging this arrow.
If you drag this arrow to the right,
then you'll see a list of devices that are connected to the computer.
Right now I only have my Nexus 6 connected.
And I also opened up the Just Java app again, so that's why it's listed here.
Let me drag this to the left again so I have more space to see the logs.
This right hand side is where the log messages are shown.
If you click on this drop-down and say no filters,
then it shows you the log messages for the whole system and other apps.
It talks about things like activity manager, audio, sound,
microphone, and all these other things.
A system log is kind of like a diary that your phone keeps about what's
happening on the device.
We also call it logcat.
If you Google for
what is logcat, then a definition pops up from the Android Developers webpage.
It talks about how logcat is the Android logging system,
where you can collect and view Debug output.
Going back to the logs, the most recent events happen at the very bottom here.
So if you want to look back in time,
you may have to scroll up to see what you're looking for.
Let's change the filter back to our app, Just Java.
This significantly narrows down the amount of log messages
that are just relevant to our app.
This makes it a lot easier to read.
When a crash happens on the device,
the description of what the phone was trying to do gets printed to the logs.
Here's our crash in the red text color.
This is known as looking at the stack trace,
because it traces back to where the error originated from.
We know that it's our app because it's our process,
which is com.example.android.justjava.
For our app, the process name matches the package name, but
it doesn't always have to match.
We haven't really learned about processes,
but, essentially, this is just identifying that this is our app.
Now, there's a lot of text in this stack trace.
And you don't have to read all of it.
We just want to scan for
the information that would be relevant to help us solve our problem.
We should look for anything in the stack trace that's talking about
either the methods or the files that we're working on.
So we'll keep a lookout for the file name, main activity.
So let's start reading the error message.
I skip over this part and
just read, IllegalStateException, Could not find the method submitOrder.
The error message goes on to further explain why it was in an invalid state.
And the reason is because it could not find the method submitOrder in
the activity, MainActivity, for
the onClick handler on the view class button.
There's a lot of words in that sentence and it may not make a lot of sense.
But there's a couple of good clues here.
It mentions button and we only have one button in our app, so
we can assume that it's probably related to that button.
Then it talks about not being able to find a method,
submitOrder, in the activity.
Looking back at our button code, we did specify that when the button is pressed,
it should trigger a method called submitOrder.
So let's look at that now.
I'm going to drag this The DDMS window down and
open up our activity_main.xml file.
So here is the button XML element.
And indeed we said android:onClick should trigger the method submitOrder.
If I go back into the main activity,
then I see that submitOrder is no longer a method, instead,
we changed it to submitOrders, so that's why the problem is happening.
If I go ahead and erase the extra s, now there's a submitOrder method
in the main activity, and the app should work again.
When I run the app again and
I and I hit the order button, then the app doesn't crash anymore, woohoo.
Go ahead and try to create this crash in your app
by changing the name of the submitOrder method.
Then check the logs for the stack trace, and
then read the error message carefully.
Then go ahead and fix the error so that your app works again.
Check this box when you're done.
shows a dialogue message saying that the app has just stopped.
It happens to Android developers many, many times a day.
Even the most experienced ones.
Sometimes the problem is due to a typo in their code, or a silly mistake.
Other times it can be due to a fundamental misunderstanding
of how to write the code for a certain feature.
While your reaction may be to panic, the voice of reason inside your head
should tell you yo go ahead and try to debug the app.
In this video we're going to learn how to debug Android app crashes.
To debug means to identify and fix bugs or errors in your app.
If it's a really obvious error like a java syntax error,
then Android's DU can catch that for you and
show a red error before it tries to install the app on your phone.
This is called the compile time error because it will fail to create any app
to even install on your device.
If you want to try creating a compile time error, go ahead and just delete
this parentheses or this semicolon and then you'll see the error pop up.
This is like in lesson one when we tried to create an XML error intentionally.
And then we read the error message and then we undid the change.
Other potential errors are if I try to delete this opening brace, then I'll get
an error, or if I forget this closing brace, then I also get an error.
But sometimes the error only happens when you start using the app.
This is called a run time error because it happens when your app is running.
Let's intentionally create a crash in the app and
figure out the steps of how to solve it, so that when you have a real crash,
you'll know how to tackle it on your own.
This is actually based off of a real crash that I saw a student encounter.
So it's very likely to happen to beginners.
I'll first walk you through all the steps and
then you'll have a chance to do it yourself.
To intentionally create a crash in the app,
I'm going to modify the MainActivity Java file.
I'll change the name of this method to be submitOrders with a s.
Then I'm going to run it on the device.
Everything looks fine at first, but
then when I click on the ORDER button, it crashes.
It gives me the message, unfortunately, the app named, Just Java has stopped.
And the only option that I have is to hit OK to exit the app.
Fortunately, we can use Android Studio to figure out what actually happened.
When you run the app on your device,
you may see this Android DDMS window pop up at the bottom of the screen.
This show you information to monitor your Android device.
All the stuff that goes on behind the scenes that users don't see.
If it's not already open, you can click on this tab down here that says Android,
and that should open up Android DDMS for you.
You can adjust the height of this window by dragging this arrow.
If you drag this arrow to the right,
then you'll see a list of devices that are connected to the computer.
Right now I only have my Nexus 6 connected.
And I also opened up the Just Java app again, so that's why it's listed here.
Let me drag this to the left again so I have more space to see the logs.
This right hand side is where the log messages are shown.
If you click on this drop-down and say no filters,
then it shows you the log messages for the whole system and other apps.
It talks about things like activity manager, audio, sound,
microphone, and all these other things.
A system log is kind of like a diary that your phone keeps about what's
happening on the device.
We also call it logcat.
If you Google for
what is logcat, then a definition pops up from the Android Developers webpage.
It talks about how logcat is the Android logging system,
where you can collect and view Debug output.
Going back to the logs, the most recent events happen at the very bottom here.
So if you want to look back in time,
you may have to scroll up to see what you're looking for.
Let's change the filter back to our app, Just Java.
This significantly narrows down the amount of log messages
that are just relevant to our app.
This makes it a lot easier to read.
When a crash happens on the device,
the description of what the phone was trying to do gets printed to the logs.
Here's our crash in the red text color.
This is known as looking at the stack trace,
because it traces back to where the error originated from.
We know that it's our app because it's our process,
which is com.example.android.justjava.
For our app, the process name matches the package name, but
it doesn't always have to match.
We haven't really learned about processes,
but, essentially, this is just identifying that this is our app.
Now, there's a lot of text in this stack trace.
And you don't have to read all of it.
We just want to scan for
the information that would be relevant to help us solve our problem.
We should look for anything in the stack trace that's talking about
either the methods or the files that we're working on.
So we'll keep a lookout for the file name, main activity.
So let's start reading the error message.
I skip over this part and
just read, IllegalStateException, Could not find the method submitOrder.
The error message goes on to further explain why it was in an invalid state.
And the reason is because it could not find the method submitOrder in
the activity, MainActivity, for
the onClick handler on the view class button.
There's a lot of words in that sentence and it may not make a lot of sense.
But there's a couple of good clues here.
It mentions button and we only have one button in our app, so
we can assume that it's probably related to that button.
Then it talks about not being able to find a method,
submitOrder, in the activity.
Looking back at our button code, we did specify that when the button is pressed,
it should trigger a method called submitOrder.
So let's look at that now.
I'm going to drag this The DDMS window down and
open up our activity_main.xml file.
So here is the button XML element.
And indeed we said android:onClick should trigger the method submitOrder.
If I go back into the main activity,
then I see that submitOrder is no longer a method, instead,
we changed it to submitOrders, so that's why the problem is happening.
If I go ahead and erase the extra s, now there's a submitOrder method
in the main activity, and the app should work again.
When I run the app again and
I and I hit the order button, then the app doesn't crash anymore, woohoo.
Go ahead and try to create this crash in your app
by changing the name of the submitOrder method.
Then check the logs for the stack trace, and
then read the error message carefully.
Then go ahead and fix the error so that your app works again.
Check this box when you're done.