Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Testing and Debugging Making a Toast

heinz meza
heinz meza
1,121 Points

not sure how to solve this: Toast allDoneToast; Toast.makeText(this, "All done!", Toast.LENGTH_LONG).show();

How to solve this? ...I'm getting an error. Please Help!!!

Toast allDoneToast;

Toast.makeText(this, "All done!", Toast.LENGTH_LONG).show();

ToastTest.java
Toast allDoneToast;

Toast.makeText(this, "All done!", Toast.LENGTH_LONG).show();
Toast allDoneToast;
allDoneToast = Toast.makeText(this, "All done!", Toast.LENGTH_LONG).show();

I believe it's not working because you need to reference the Toast variable "allDoneToast" you just declared. Other than that your syntax looks correct.

For any further info on Toasts, you can refer to the link below, which directs you to the android docs. http://developer.android.com/guide/topics/ui/notifiers/toasts.html

1 Answer

Hi there,

There's two steps to this.

First, set up the Toast and store it in the variable provided. Then call the .show() method on the variable:

Toast allDoneToast = Toast.makeText(this, "All done!", Toast.LENGTH_LONG);
allDoneToast.show();

Hope that helps .

Steve.

The last part of the challenge then requires you to combine the Toast call into one line - that's what you did in the second line of your initial post.

Steve, would my syntax have worked as well? Can you concatenate the .show() method on the end like that?

I don't think so as it would fire the Toast too soon.. The .show() method displays the Toast; you called that immediately so, whilst in this set of code the toast was called immediately after it was created, it is possible that the Toast would be further called elsewhere in the code.

I'm not sure what your code actually assigns to the variable allDoneToast - I don't know what's left after the .show() method is called. So it is possible that the contents of allDoneToast aren't what's expected for the code to operate as intended.

Make sense?

Steve.

How does the java compiler read java code? Left to right?

In my example I didn't assign the allDoneToast variable to anything I was just declaring it. Then initializing it below. Which is what I believe @heinz meza was trying to do, the only thing that looked wrong about their syntax was they never assigned the allDoneToast variable to anything, they just declared it.

Not wholly sure. I would imagine it'll take the code a line at a time within each code block. But, yes, for our ease of use implying a left to right reading style makes sense - certainly for the direction of assignments etc.

So, x = y, assigns the value of y into x.