Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Right now our app crashes at the end of the story. Let's fix it! We'll use the isFinal() method to control how the buttons are displayed and used on the final pages of the story.
GitHub Repo
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Everything is good here except
the last page of our story.
0:00
If we try to go all the way to one
of those right now then we'll get
0:03
an exception because we don't have
any data in our choice objects.
0:06
Ugh, unhandled exceptions are the worst.
0:10
Let's fix it.
0:12
Hopefully you were able to take a look
at the log and see what the error was.
0:14
Let's go back to Android Studio and
0:18
bring up the logcat view with
the Android Monitor tab down here.
0:19
And we can see pretty quickly here at
the bottom that we're running into a null
0:24
pointer exception.
0:27
If we keep reading,
we can find out that it's happening
0:29
when we try to call getTextID
on a null object here.
0:31
So basically what is happening is
when we get to one of our pages that
0:36
doesn't have any choice objects,
we're getting this null pointer exception.
0:39
Remember, the last page of the story
we decided won't have any choices.
0:43
And instead have a boolean flag that
says if it's the final page or not.
0:46
Okay, let's fix it.
0:50
Just for quick review,
let's take a look at the page class.
0:51
So let's close this, open up Page.
0:54
And remember,
we added this isFinalPage flag and
0:56
some methods that tells us
if it's a final page or not.
0:59
And right now, if we scroll down here,
it says that it is not being used.
1:02
So let's use it.
1:06
Let's go back to StoryActivity.
1:07
So back here in our loadPage method is
where we are setting all the information
1:09
related to the choice buttons.
1:13
So here, before line 61,
let's add a new line, and
1:15
a check if (page.isFinalPage()).
1:19
And then inside here,
if it's the final page,
1:24
we want to execute some code to handle
the buttons a little bit differently.
1:26
If it's not final then we still want
to do the rest of all this work.
1:30
So let's make that the else block.
1:33
We'll add else, curly brace,
scroll down a little bit and
1:35
I'll put another curly brace down here.
1:37
And everything is indented automatically,
thank you Android Studio.
1:41
Actually, this loadPage method is
getting a little bit long for my taste.
1:45
It's doing too much work at this point.
1:48
Let's refactor this else block into
a new method just to load the buttons.
1:50
So I'm going to select everything in here,
click on refactor,
1:54
extract, method.
1:58
And let's call this loadButtons and
click OK.
2:02
All right, so
coming back to our if statement,
2:07
how exactly should we handle this
situation of the final pages?
2:10
Well, let's take a look
at the mock ups again.
2:13
Our designer and
I talked about this specific scenario.
2:15
And you can see here
on the last two pages,
2:17
we decided to have one button
that just says, Play Again.
2:20
Tapping on that will take the user
back to the initial screen.
2:24
So, to accomplish this, we are going
to have to hide the top button, and
2:27
then change the text on the bottom button,
and change its target.
2:30
Hiding the button is really easy.
2:34
There are two different ways we can do it.
2:35
The choice one button is the top button.
2:37
So let's do choice1Button.,
and then we'll use
2:39
a method called setVisibility,
and you can guess what it does.
2:43
It sets things to visible or invisible.
2:47
Now the parameter isn't in parameter, but
2:49
there are a couple different constants
in the view class that we can use.
2:51
So start typing view with a capital V,
dot, and
2:54
we have VISIBLE and
INVISIBLE right here at the top.
2:58
These two should be pretty
obvious about what they do.
3:00
They make the view either visible or
invisible.
3:02
There's another one, View.gone,
3:06
that removes the view
completely from it's parent.
3:08
Which can effect how things
are laid out on the screen.
3:10
Especially if other views are positioned
relative to the view that we call this on.
3:12
So we wanna make sure
we use the right one.
3:16
We just wanna make this INVISIBLE.
3:18
Next we want to set the text of the other
button, the one that is still visible.
3:21
So we can go choice2Button.setText and
3:25
the text in this case
is simply "PLAY AGAIN".
3:29
And check this out.
3:36
We can refactor from code directly
into a string resource too.
3:37
So it's highlighted in yellow for
us, we can hit Alt + Enter,
3:40
go down here to extract string resource,
and for the resource name let's call this.
3:43
Play_again_button_text.
3:49
And there we go, it gets popped in
here instead of the hard coded string.
3:52
Okay, let's run this and see how it works.
3:57
All right,
let's keep testing different names.
4:00
We'll have Brittney start
the story this time, and
4:05
let's go through to one of the end points.
4:07
Explore the coordinates, and
there we go, PLAY AGAIN.
4:10
And if we tap on the button it takes us to
another page, that's because the on click
4:12
listener for the button was still
pointing to the old target.
4:17
Now if I tap on it here
nothing will happen.
4:21
All right, so
we have two final things to take care of.
4:22
We need to make this PLAY AGAIN
button work correctly and
4:25
take us back to the start, and
we need to fix our back navigation.
4:27
Check this out.
If we hit the back button,
4:31
instead of going back
through the pages of a story,
4:33
it's going to take us
back to the beginning.
4:35
That's not what we want.
4:38
Let's break for a moment.
4:39
And then when we come back, we will
tackle the PLAY AGAIN button first.
4:39
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up