Hey there.
Well let's apply our knowledge of variables in our application.
The last time we implemented a behavior in our application that when you click the plus button displays
number one.
This is cool but in a real application we need to increase the quantity by one.
Each time we press the button plus let's now implement the desired behavior using a variable words create
a quantity variable an integer type or variable into quantity and set it to 0.
Next when you click the plus button the increased quantity method is called as you remember.
Let's take a look to the activity main file blast button.
Here it is.
And as you can see on click method has said increase quantity.
Here is this method in main activity.
Let's increase the quantity value in it while 1 this is done this way.
Quantity assign quantity plus 1
and next in our text to you.
We're not the number 1 but the value over the quantity variable.
Now let's run our application so the application has a restarted press the plus button.
And as you can see we get message music shop has stopped.
Why did this happen our application was stopped abnormally.
I don't understand the reason we need to do the debugging debugging is the search for errors in the
application their base or work the body is a word bug that is we are looking for bad bugs in our code.
There are several ways to detect errors in the application.
First of all you need to look at the so-called low cat down here.
You can see the low cut tab.
Let's click on it to open it can be increased for convenience.
This is current all notes in which all events appearing in the device are recorded.
There are different modes of these or cut variables.
This is the most verbose mode that is.
It contains all the events.
Also here the bug in for war error and assert.
Now we need the error mode since we are looking for an error in our application.
Click on it and scroll the low cut just a ball.
As you can see it says illegal state exception could not execute method for Android on click.
And also written line quote of the main activity number twenty two and the method of increase quantity
which contains these bug we can click on this link and Android Studio redirects us through this method
increase quantity to these particular line.
The error is contained here.
Quantity text you said text quantity.
Why did this happen.
This happened because their method set text takes strings as an argument and we passed that quantitative
variable which as you remember let's call ups are low cut as you remember this variable of integer type
int variable.
And here you need to pass this screen but there is a way out of this situation.
We can concatenate our variable quantity with a string that is we can specify to double quotes here.
Then put a plus sign and concatenate the quantity variable to these empty string.
As you remember when a string is concatenated with an integer or with any other number the result will
be a string.
Therefore after this our application should function normally.
Let's now try to run our application
alerts.
Click on the plus button and cool everything worked out our button increases the value by a one each
time so the body I want to repeat again it is done by searching for error messages in the.
At this type of error that we examined is called Iran time error program around time error.
That is it was found when the application was already running.
There are also compilation errors that under its to do determines immediately before the application
runs on the emulator or on their real device.
For example if we remove their semicolon here androids to the immediately underlined it with a red line
and writes that the semicolon is required.
If we don't pay attention to it and launch their application then we get a so-called compilation error
compilation failed.
And here we can read what's the matter.
As you can see here it says Error expected column.
Let's get semicolon back in place and let's now implement the behavior for the minus Bolton.
Let's go to the X ammo file activity Main and create a method for the minus button that on click method
and call it decrease quantity
click on the red line ball and click on the create decrease quantity in main activity and we can copy
the entire body of the increase quantity method and paste it into the decrease quantity method.
With only one difference.
Quantity is now reduced by 1 that is.
Here we are right quantity equal to quantity minus 1.
Let's run our application again and see if everything works correctly for us
let's press the plus button it works.
And now the minus.
And as you can see it also works value of quantity increases and decreases.
Wonderful but there is one point.
Our quantity can take a negative value in real life.
The quantity cannot be negative.
Therefore we need to fix our code in order to implement the current behavior we need.
If our variable quantity takes a negative value do not reduce this value.
But for this we need to get acquainted with the conditional Java operators which we will do in the next
lecture.