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

John Griswold
John Griswold
2,184 Points

I don't understand the error (this is like dealing with gcc error messages!)

the gripe is to use "this" as the first parameter: Toast allDoneToast = new Toast(); allDoneToast.makeText( this, "All done!", Toast.LENGTH_LONG).show();

And it looks to me as if I do. What extremely simple concept am I not grasping?

Thanks, John

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

3 Answers

Hello,

The reason your method isn't working is that makeText isn't execpted to be called using an object as it returns a created Toast object and doesn't modify the one it is called from if it is called from one. To do it your way by first creating a new Toast, you would need to do it like

Toast myToast = new Toast(this);  // this being the context
myToast.setText("All done!");
myToast.setDuration(Toast.LENGTH_LONG);

However, we can remove most of those lines by using the makeToast method like this(while still saving it)

Toast myToast = Toast.makeText(this, "All done!", Toast.LENGTH_LONG);
Ben Junya
Ben Junya
12,365 Points

When it comes to Toasts, I personally like to use just one line of code, as I mainly use them for debugging.

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

Give that a shot and see what happens.

I would only use the other method that you tried if I had a certain toast that would be reused over and over again.

John Griswold
John Griswold
2,184 Points

Thanks, but I'm trying to follow the course quiz, which has this predicate: 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.

(code) toast allDoneToast;

so I instantiated toast, and tried the code displayed. The compiler grope is that use of this as the first parameter, which I did. I suspect the grading code under the question is broken. Fundamentally, I agree with your solution, but I want to know why mine doesn't work.

best, John

John Griswold
John Griswold
2,184 Points

Thank you - the gripe was not having a this in the new call. That was the simple bit that was eluding me.

Thank you very much. I'm an old assembly-language programmer trying to retreat my old brain.