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

Nathan Pickard
Nathan Pickard
10,950 Points

Compiler error for Toast challenge

The instructions read:

"Set the Toast variable using the static makeText() method of the Toast class. Use the this keyword for the first parameter, the text "All done!" for the second parameter, and the constant Toast.LENGTH_LONG for the third parameter."

I don't understand why my code is getting a compiler error here. I tried switching components around, but no luck in understanding what the code is actually doing.

ToastTest.java
Toast allDoneToast;

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

Try using Toast.LENGTH_LONG instead of allDoneToast.LENGTH_LONG, because that is what they said in the question. Your way works perfectly fine, but this isn't a real compiler :-P just a simulation so it might be buggy.

2 Answers

Nathan Pickard
Nathan Pickard
10,950 Points

Ok, it seems the compiler for this challenge may be bugged for me, not sure. Anyway, here is what my code looks like:

Toast allDoneToast;

allDoneToast.makeText(this, "Yay! Our Activity was created!", Toast.LENGTH_LONG);

And here is what the error is saying:

"JavaTester.java:47: error: variable allDoneToast might not have been initialized allDoneToast.makeText(this, "Yay! Our Activity was created!", Toast.LENGTH_LONG); ^ 1 error

It's my understanding that the "allDoneToast" variable was already initialized. What gives?

makeText is a static method of Toast.

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

Assign this to allDoneToast.

Hope this helps. :)

Nathan Pickard
Nathan Pickard
10,950 Points

That did the job Adam! It said it wasn't initialized, so that did the job, thanks!