Unfortunately App Has Stopped - How to Fix - ONLY FOR PROGRAMMERS - Android Studio Tutorial

Unfortunately App Has Stopped - How to Fix - ONLY FOR PROGRAMMERS - Android Studio Tutorial


Unfortunately App Has Stopped - How to Fix - ONLY FOR PROGRAMMERS - Android Studio Tutorial

If your Android app crashes, then because of an uncaught RuntimeException like a NullPointerException.
In this video we will learn, how to read the Logcat stack trace to find out what exception caused our app to crash and where it happened in our code, so we can provide more specific information when we ask for help.

Java RuntimeException documentation:
https://docs.oracle.com/javase/7/docs

____________________
💻 Find the BEST programming tutorials on TutHub:
https://tuthub.io

⭐ Get my MVVM Caching Course now:
https://codinginflow.com/caching

❗ Subscribe to the channel:
   / codinginflo.  .

📨 Subscribe to the Coding in Flow newsletter:
https://codinginflow.com/newsletter

❓ Join our free developer community:
https://discord.gg/TSnMvmc

📣 Follow Coding in Flow on other sites:
Facebook: https://www.facebook.com/codinginflow
Instagram: https://www.instagram.com/codinginflow
TikTok: https://www.tiktok.com/@codinginflow
Twitter: https://twitter.com/codinginflow
Github: https://github.com/codinginflow

💰 Business requests, sponsoring, etc.: [email protected]


Content

0.49 -> Hey guys. Welcome to Coding in Flow
2.4 -> In this video we will learn how to read the stack trace
4.93 -> to find out why an app is crashing
7.23 -> Because often under videos I see people commenting some like this
10.73 -> "I tried your code but my app is crashing, what should I do?"
13.88 -> Or "When I click this button I get a crash. How can I solve this?"
17.59 -> And the problem with these questions is
19.25 -> that this is not enough information to help you
21.6 -> Because there could be countless reasons why an app is crashing
24.69 -> So in this video we will learn how to get more information about our crash,
27.93 -> to ask more specific questions
30.35 -> In general an app crashes because of uncaught RuntimeException
34.13 -> Which basically means that we try to do something that is not possible
37.44 -> A very common RuntimeException is the NullPointerException
40.75 -> So let's take a look at an example
42.81 -> I create a private TextView variable and call it "textView"
46.67 -> And now in our onCreate() method I call "textView.setText()" and pass a text
54.07 -> When we run this app it will throw a NullPointerException
56.57 -> because we forgot to assign our "textView" variable
59.35 -> We didn't call findViewById() or "new TextView()"
62.39 -> So right now our "textView" variable is null
64.52 -> and we can't call setText() on a variable that has no reference to anything
69.05 -> So let's run this
71.81 -> And as we expected our app crashed
74.07 -> Now the wrong thing to do is to go to a video or Facebook group
77.11 -> or StackOverflow and write
79.13 -> "My app is crashing I don't know why. What should I do?"
82.05 -> And it's also not a good idea to just post your whole source code there
85.93 -> or a link to your project on Github because it's unlikely
88.79 -> that someone else wants to take his time to unravel it
91.49 -> So people will usually will tell you to look in to Logcat
94.12 -> However you should also not just copy-paste
96.2 -> your whole Logcat output and post it somewhere
98.57 -> Instead you have to look what caused the exception
101.7 -> So down here we click on Logcat
103.64 -> or alternatively we can press [Alt + 6]
107.6 -> And there we can already see our red text
109.79 -> If you don't find any red text here than make sure
111.96 -> that you are in the correct emulator up here
114.69 -> and in the correct app
117.29 -> Also make sure that you don't have any filters at here
119.66 -> that filter out your error messages
122.01 -> For example the "Firebase" one
123.69 -> But "Show only selected application" is fine
125.99 -> And if you have too much text in here you can change this
128.63 -> from "Verbose" to "Error"
131.55 -> And this is called the stack trace
133.21 -> It's basically a list of method calls that app was executing
136.63 -> when an exception was thrown. So this way we don't only see
140.01 -> where our error happened, but also how our program ended up there
144.58 -> This is obviously a very simple situation
146.91 -> Up here we can see that we have a "RuntimeException"
149.55 -> which is the parent class. And here we can see
151.97 -> Caused by: NullPointerException
154.22 -> This "Caused by:" is what we are interested in
157.51 -> And when we look here we can see
159.33 -> "Attempt to invoke virtual method .. TextView.setText()
162.32 -> .. on a null object reference"
164.27 -> And this pretty much already explains what our error is caused by
167.7 -> And as you can there is only one link that is blue
170.23 -> This link is blue because it points to our code
172.8 -> While the other links are greyed-out because they
174.59 -> point to code from the framework
176.39 -> So when we click on this link we get to our "textView.setText()" line
180.15 -> and this is where the error happened
182.09 -> Now usually from the type of the exception and the method
184.77 -> you were trying to execute you can draw your own conclusion why this error happened
188.79 -> setText() on the null object reference means our "textView" is null
192.41 -> because we forgot to assign it
193.99 -> If you still can't find out what exactly went wrong
196.4 -> you can copy-paste the relevant error messages
199.15 -> and the part of your code where it happened
201.35 -> and anything else that might be relevant on StackOverflow
204.14 -> or in a community or under a video
206.41 -> But now people can better help you because they have
208.52 -> a specific error message and the relevant parts of your code
212.03 -> But of course first you should try to find your answer on Google
215.55 -> Now this was the very simple example
217.63 -> Let's say instead of calling this directly in our onCreate() method
221.56 -> we put this TextView into a separate method which we call "changeText()"
228.52 -> And we create this method below
234.12 -> And this method calls changeText2()
239.16 -> which we also create below
243.75 -> And in here we try to change our text
245.79 -> So let's run this again
248.32 -> And this time we have multiple blue links
250.5 -> Because we tried to call multiple methods in our own code
253.57 -> until this error happened. And in this case you usually want to
256.56 -> look for the top most one because this will point you
259.91 -> to the root of the error
262.24 -> Now in some situations we will have multiple "Caused by:"s
265.82 -> For example when we surround this with a "try/catch block"
271.07 -> So we try to execute this
274.38 -> We will catch the NullPointerException
277.35 -> but in here we will throw
280.3 -> a new exception. For example an "IllegalStateException()"
284.63 -> Of course this is not a real world example
286.28 -> this is just to show you how this works
288.49 -> In here we will show a message
290.42 -> And I'm gonna write "This is not possible"
294.04 -> Obviously in a real app you should be much more specific
297.15 -> And as the cause we pass "e" which was our NullPointerException
301.76 -> When we run this again
303.79 -> We now have two "Caused by:"s
305.96 -> So we have a RuntimeException which was caused by our IllegalStateException
310.88 -> which says "This in not possible"
313.27 -> And this on the other hand was caused by our NullPointerException
316.73 -> which was the root cause
318.57 -> So usually we want to look at the lowest "Caused by:"
321.61 -> and then at the topmost "at" which contains our package name and the blue link
326.65 -> If we can't find our package name in this "Caused by:"
329.55 -> then we look at the one above it and see if we find our package name there
334.15 -> So you usually want to look at the topmost appearance of your package name
338.08 -> in the lowest "Caused by:". And again when we click here
341.8 -> We get to our .setText() line
344.13 -> So this was a general introduction into how to read the stack trace
347.69 -> And if you see someone asking why his app crashes
349.93 -> without any specific information - do me a favour and share this video
353.58 -> If this was helpful, please leave a like
355.11 -> and if you want more Android tutorials don't forget to subscribe
357.83 -> Take care.

Source: https://www.youtube.com/watch?v=zUiJ4Qig_Ic