Solve Common Problems
Learn about common problems you might encounter while building your apps and see how to solve them.
One of the biggest pitfalls when you’re first learning app development is understanding what to do if something goes wrong - and believe me, things will go wrong at some point. That’s not a bad thing if you know how to solve the problems. These problems or mistakes are commonly known as bugs.
And that’s what this episode covers. You’ll learn how to solve some common beginner mistakes. Now, I’ll recommend you watch this episode even though you’ve successfully completed previous exercises. Because chances are you could encounter some of these types of problems as you progress in your app development journey.
Alright, let’s dive in!!!
To get started continue with the current project you’ve been working on or open up the starter project for this episode. Let’s go through some common beginner mistakes you might make when you’re getting used to kotlin and android development.
Spelling mistakes are common during development.
So if i delete the
t from the
showResult() function call inside the hit me button’s click listener.
You can see it turns red.
And if you hover over the error, you’ll see some message pops up saying “Unresolved reference.”
This simply means that Android Studio couldn’t find anywhere in the file that a method with that spelling was defined.
So trying to reference it here is causing a problem.
We can also access all problems in our project via the problems tab in the status bar below.
Go ahead and open it up.
In here, you’ll see a list of all the problems and their corresponding line number in the file.
I’ll go ahead and add that
t back to the
showResult() function call.
Next, the kotlin programming language is case sensitive. This means that an uppercase letter is different from a lowercase one.
So for instance changing the letter
ActivityBinding to a lowercase
a will produce an error.
This is because Kotlin sees them as two different indentifiers.
You get the same “Unresolved reference” error message.
I’ll go ahead and chage that back.
Another thing that could cause a handful of unexpected problems is missing parenthesis.
Errors that occur from missing parenthesis can be tricky to trace.
I’ll go ahead and remove the closing parenthesis of the
And if you look at the problems panel below, we have a plethora of errors.
The simple trick to solve these type of errors is to always start from the top.
I’ll double click on the first error and it takes us to the code around the
setContentView function call.
By just looking at that section, you can see that it needs a closing parenthesis.
The error shows that the function cant be called with the arguments supplied.
And this is caused because kotlin cant find a closing brace so it continues looking down the file for the expected
argument that the function call needs.
I’ll add back the closing parenthesis.
The same also applies for curly braces which signify the start of a code block like the declaration of a function’s body.
To avoid this type of error, the rule of thumb is to always close a curly brace or parenthesis immediately after you open one. Android Studio helps you with this though. Whenever you open a parenthesis or a curly brace, the IDE’s intellisense automatically closes them for you. But these errors can still occur when you copy and paste code.
Finally, the location where you declare your variable matters.
Let’s move the declaration of the
sliderValue variable inside the
onCreate function of the activity.
If you scroll down to where it is used inside the
showResult function, you can see we have unresolved reference error.
And this is because the
sliderValue is now local to the
Meaning that the rest of the
MainActivity class wont have access to it.
This is known as variable scope.
sliderValue variable is now a local variable of
You’ll learn more about scope as you explore the kotlin programming language.
I’ll move that line of code back up now.
Android studio will always try its best to give you a reasonable message and suggestion. But whenever something shows red, start at the topmost error, go to the code on that line, and trace downwards. But often, Android studio will point you in the right direction to help you figure out what’s going on.
So far in this episode, we’ve been dealing with errors - which are in red, but sometimes Android Stuido will give you a warning, which is in yellow.
Now, what’s the difference? Well, errors are fatal. If you get one, you cannot run the app until the error is fixed.
On the other hand, warnings are informative. We’ve come across several warnings in earlier episodes.
Like when Android Studio warned us about hardcoding text inside textviews. Or when Android Studio warned us about the structure of the hit me button’s click listener. Telling us we could move the expression out of the parenthesis.
As you can see, for warnings, your code will still run but Android Studio warns and informs you that there is a better way of doing things.
Personally, I’ll suggest you treat most warnings as errors and try fix them before moving on.
Now this doesn’t guaranty that your app won’t have any bugs, but at least they wont be silly ones.