SOLVING EVERY ERROR IN ANDROID STUDIO - Android Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to a new video so if you're a beginner and just learning Android development this video might be the most important programming video ever for you because as you maybe know I have an Instagram page and there I am regularly asked how to solve specific errors and most of those errors you can solve with only 10 seconds of googling and because I think that many beginners don't know what kind of aura errored they even encounter and how they should google it appropriately I want to show you this in this video here so make sure to watch this video till the end it will save you a lot of time as you can see I wrote a simple algorithm here that should just sort this list here and I deliberately made an error here so if you're a little bit more experienced you might find that arrow right away but if not then let's imagine you had this algorithm and you don't know what the error is now I want to show you how we can actually find out what the error is and how to solve it Android studio provides a tool which is logcat which you can find below here so let's click on that you open it and logcat is basically the console for Android studio and it is your best friend when you want to find errors in your code and inside of logcat it shows all the locks that have something to do with your app so on the left you can see your current selected emulator so you listen now for all the locks that are on that emulator and that have something to do with your application so in my case it's con that Android dev start tutorials so that is this package for this particular app so we only listen to the logs that come from this app to the right of that you can see a little drop down menu that says verbose and at that drop down menu you can select the current lock level the lock level is just kind of a filter for your log messages so it determines which messages are shown in that lock window here and verbose means that it simply shows all available log messages debug means it only shows log messages related to development info is just for just for regular usage and it's always the same that if you select the log level for example info then all the info logs are shown and all the locks of the levels below that but for log level info the debug locks are not shown for example but if you select that debug log then the debug log messages are shown and all the log messages that belong to the levels below so in general you will most likely need the debug filter or that error filter if you only want to filter for errors I will just keep it at verbose here to just show you all the log messages and currently we don't even have any log messages because our application is not running so let's change that by clicking on that play symbol above here and now you can see a lot of log messages appear here that have something to do with our application 90% of those are actually unnecessary or don't tell you anything that you need to know at the current moment but the important part for us is as I told you I deliberately made an error here in my algorithm and it throws us a big error message here and if you're a beginner you might just think what the and that was the same that I thought when I was a beginner but it's actually not that hard to understand if you once learn how to read that messages and I think not many people who make tutorials really show you how to solve your errors and how to read those messages but it's really important because when you are in your programmer then you encounter errors all the time and you must learn how to understand that understand those error messages and how to Google for it so let's start to decrypt that huge error message in general such an error message is shown to you when summer in your program and exception is thrown and that is exactly the case here so if we take a look first it tells us fatal exception main that doesn't really help us then which process that exception appeared in doesn't help us to because we know that it appeared in our app and if you read the next line it tells us runtime exception unable to start activity and so on blah blah blah that doesn't even help us to but after that the important part starts which is Java dot Lang array index out of bounds exception and that is actually the exception that occurred here and it tells us that we wanted to access an index of an array or of a list that didn't exist so you can see that length of our list is six here and we wanted to access the sixth index of that list but because the length of that list is six the highest index of it is five and because of that the error is thrown here and if you scroll down a little bit then almost every time such an error message is shown to you there are some blue links here and the blue links are actually those links that are important for us because they belong to our project the grey links belong to the Java framework or the android framework and we cannot see implementations of those classes so let's take a look at the bottom blue link here and you can see it relates us to our uncreate function in main activity so let's click on that and it will move the carriage to the line where that error occurred but in that case it just tells us that the error occurred in that short list function and it doesn't tell us where exactly in that short list function that error occurred and to let Android studio show us dead you have to click that top link because if you take a look at the left of that it tells us that this link will lead you to the saltless function in line 21 so let's click on that and it takes us to the exact line in which the index out of bounds error accrued here and now we know that the error must have appeared inside of that if statement because that is the place where we access the list at a specific index and now we have to think by ourselves why that index out of bounds error can appear here so let's check which indexes we actually access here we only access indexes that are related to J so let's take a look that J goes from 0 to list out size minus 1 so our list is has the size 6 so it goes from 0 to 5 and the highest index of the list is 5 and the highest number that Jake and that Jake can be here is 5 2 and if we access that index at the highest number that Jake can be so lists at J plus 1 so at 5 plus 1 that means that we access an index here that is 6 and that list does not have an index data 6 and that is actually what the error message here told us that we wanted to access the index 6 in that list that is only that has only the length of 6 but let's say you don't know right away what that particular exception tells you because our example is pretty obvious that the error in X is just out of bounds here but there are many exceptions that don't tell you right away what the problem is and in that case you just want to copy that exception type go to Google Google it click the first Stack Overflow link here and scroll through the answer and most of the times there's just a good guide that explains it in very detail and I think in Stack Overflow the explanations are mostly just much better for beginners then if you look into the Java or Kotlin documentation for example so let's actually jump back into Android studio and solve that error the error was that we access the index sixth at that position here in the code and to solve it we just need to go to list dot size minus 2 so we don't the highest index that we access here in that square bracket is 5 and maybe you've noticed that I started the list and then I wanted to print it afterwards that is the way we did it in the cotton newbie to Pro Series because in that console we needed to print something all the time with the print line function but now if we want to use lock yet of Android then the print line function works so if I just run the program here then you can see a lot of log messages and here's actually our print line function that just prints the lists in sorted order but a better way to actually print something into Lochhead is to use the functions that are created for that locket and in Android in the android framework you can just use a lock dot and then you can just select the current lock level you want to lock that message on so d stands for debug eve for error and so on you have I here info verbose as I just explained those different levels before and let's just lock something here in debug that is the most common lock level you will use so let's use that and then you can see it requires a tag and a message and the tag is just used to filter that lock so it will give your lock message attack that you can just search for in that lock head window here let's use for that tag main activity so we know that this lock message was locked from within main activity and after that we can just print something like hello this is our first log message and if we now run that app wait a little bit then rocket will reload and show all the new log messages and here's our list that we are sorted and that we printed with a system that out so with the print line function and above that you can see our tech main activity and it says hello this is our first log message and let's say we didn't find that log message right away then we could filter for debug log because we lock that message at the debug level so let's click that and now it only shows you debug messages which are still a lot and then we could click on that search box here and only search for shock messages that have the name mainactivity in it and then you can see it filters all the messages and only shows us our just created lock message here in general it is a good habit you use the logging features of android studio and lock for example when particular functions are executed or when they finish so that when errors occur that those log messages help you to spot and to solve those errors that's it for this video it's really important to learn what error messages tell you and how to google them it can save you a lot of time because you don't need to ask other developers that often because you're able to help yourself if this video was helpful for you please come below and leave a like also if there's something I can improve on please tell me below that would really help me and yeah still in the next video have a good day bye bye
Info
Channel: Philipp Lackner
Views: 62,952
Rating: undefined out of 5
Keywords: error handling, error, android studio, android, programming, exception handling, kotlin, error messages, stackoverflow, coding, coder, programmer, logcat, android development
Id: 2a8PqmNKwTs
Channel Id: undefined
Length: 12min 53sec (773 seconds)
Published: Thu Jan 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.