Handling Errors7:21 with Ken Alger
An alert dialog shows a message to a user, and importantly, requires the user to dismiss it somehow before continuing. This is perfect when we want to make sure the user sees a message about an error.
Let us start by talking a bit about the tricatch block we have in our code. 0:00 The idea here is that if something bad happens in our app, the Java runtime 0:05 system will kind of raise a little flag that says something is wrong. 0:09 If we are on the lookout for these flags, we can handle them appropriately and 0:13 save our users from a crash or bad experience. 0:17 The act of raising that little flag is called throwing an exception 0:21 when we must catch them in code in our app. 0:25 A lot of methods we use throw exceptions like these. 0:28 The response.issuccesful method is one example. 0:31 It throws a specific type of exception called an IO exception. 0:35 Which stands for input output exception. 0:39 Specific exceptions like this are typically 0:43 subclasses of a base class called exception. 0:46 What can we do about them? 0:49 We need to wrap our code in something called a tri catch block, 0:51 like we have here. 0:54 Our code then is essentially saying, try to check if the response is successful. 0:59 If so, do something. 1:04 In this case, output to the log the forecast data. 1:06 If it fails with an IOException, run this other code to handle the exception. 1:09 Otherwise, we just proceed as normal. 1:15 In this specific case, we are just logging the exception. 1:17 But we could take other appropriate action as well. 1:21 Next, let's talk about how to handle the case where the response is not successful. 1:25 We want to add an else block here. 1:29 Down under the if, I like to drop this down. 1:32 That's just a matter of personal taste. 1:39 So inside here let's Log.v(TAG). 1:42 It would probably be helpful to know what the response looks like. 1:46 Response.body().string. 1:52 Now to make our code follow the dry principle of, don't repeat yourself. 1:57 Let's move the log statement up here to the top of the try statement. 2:02 We can get rid of that one. 2:18 Now it would be great to alert the user about the error somehow. 2:22 After all, they won't see the log messages in the app. 2:26 Let's create a new method inside our else statement to do just that. 2:30 Call it alertUserAboutError. 2:34 You can come over here now. 2:41 Hit Alt + Enter. 2:43 For the quick fix pop-up, we want to create a new method or 2:48 alertuUserAboutError. 2:52 And we wanna create it here in MainActivity, 2:53 not in this anonymous inner class. 2:56 We want the main activity. 2:57 Scroll up a little bit. 3:01 Inside our new method, 3:03 we want to do something to visually alert the user that an error has occurred. 3:05 We could certainly use a toast as we've seen before. 3:09 But the issue with toast method is that they only stay on the screen for 3:12 a short period of time. 3:16 If we want to make sure the user sees a message, 3:17 we should use something that requires them to confirm or dismiss it. 3:20 Android provides a nice way to do this with dialogues. 3:24 Let's go to the Android docs and check out the documentation for dialogues. 3:27 Dialogs pop up over the activity and ask the user to do something. 3:40 You're probably familiar with them in other apps, and 3:45 this is a great use for them. 3:47 They can be simple or quite complex. 3:49 We'll just build a simple one for our StoreMe app that has an error message and 3:52 an OK button. 3:55 We scroll down on this page a little bit 3:57 We read that we should use a dialog fragment as a container for our dialogs. 4:02 Now, it is kind of a lot of code for 4:06 what appears in the app as a pretty simple thing. 4:09 The good news though is that once you have this example working, 4:12 you'll have code you can easily reuse and adapt in the future. 4:15 Let's head back to Android Studio into our code and add a dialog to our app. 4:19 So first, we'll need a new class for 4:24 that dialog fragment, as mentioned in the documentation. 4:25 We can do that here in the Project pane. 4:29 Select Java, To New Java Class. 4:31 We'll call it AlertDialogFragment. 4:38 And for the super class, we will want to 4:47 put in android.app.DialogFragment. 4:52 Everything else is okay Fragments 4:57 are similar to activities and are very useful in certain situations. 5:02 We'll cover fragments in more depth in future projects. 5:06 For now, we need to override one key method from dialog fragment. 5:09 Type onCreateDialog and hit Enter for auto-complete. 5:14 The method will be called when we create the dialog. 5:19 We will do this from our activity in a moment. 5:22 We need to add code here to configure the dialog. 5:26 To do that, we will use a special dialog class called AlertDialog. 5:29 So we can delete this line here, And replace it with the following. 5:34 AlertDialog.Builder. 5:40 Builder, option enter for inputs. 5:46 And we want that first one there. 5:49 Not the support. 5:52 We want a new AlertDialog.Builder. 5:54 Now we need to pass in the context. 5:57 Normally we would use this or 5:59 activity.this to refer to the current context which has been the activity. 6:02 However, now we're in a different class so what do we use for our context? 6:08 Fortunately, the DialogFragment class that we're extending 6:13 has a useful method to get the activity where this DialogFragment was created. 6:17 We call it with getActivity which will? 6:23 Yeah, you guessed it, give us the activity and the context we need. 6:26 So we can put in, getActivity. 6:31 Let's actually make it into a variable as well, that will be useful shortly. 6:35 So context context, do our imports, Get activity. 6:43 And then we can pass in contacts down here. 6:50 Notice that our data type is alertDialog.builder. 6:58 Builder is a nested class inside the alert dialogue class. 7:02 As a quick side note here, the reasons for 7:06 this have to do with a software design pattern called the factory method pattern. 7:08 I've put links in the teacher's notes for further reading on this. 7:13 Now is a great time for a quick break. 7:16 When we get back, we'll setup this alert dialogue. 7:18
You need to sign up for Treehouse in order to download course files.Sign up