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

I'm stumped on code for the Toast Class

here is my code: Toast.makeText(this, "All Done!", Toast.LENGTH_LONG).show();

is this correct or do I need to initialize the variable whose i.d. I forget now... sigh.

4 Answers

Toast allDoneToast = Toast.makeText(this, "All done!", Toast.LENGTH_LONG);
allDoneToast.show();
Toast.makeText(this, "Yay! Our activity was created!", Toast.LENGTH_LONG).show();
Chris Wolchesky
Chris Wolchesky
12,025 Points

That looks right! You might need to specify the first argument more explicitly, E.g.,

Toast.makeText(MainActivity.this, "Toast message!", Toast.LENGTH_LONG).show();

I'm also stumped on the log creation with the tag. Seems that the answers we have look or appear correct but there is something I am overlooking. Thanks for the response. Tim

Chris Wolchesky
Chris Wolchesky
12,025 Points

Logging would use a different function, all static calls of the Log class, depending on the level of logging you wish to use. For example,

Log.d(TAG, "Clicked the button!");

Of course, this would assume you've set the TAG variable already as a member variable of the current class, such as

public static final String TAG = MainActivity.class.getSimpleName();

public static final String TAG = FunFactsActivity.class.getSimpleName();

so the example they give as a class would be like the fun facts main activity (above), but I still get an error telling me the second parameter is not correct i.e. the string type in quotes of course. weird. thanks for your help. Java is a cool program and I started working in it years ago. so all of this is review.

Chris Wolchesky
Chris Wolchesky
12,025 Points

You might want to make sure your message you're passing in is actually a string, if you're sending it a variable name instead. Java is pretty clever in using concatenation with the + operator, but sometimes for even string-like data types you still need to call some sort of .toString() method before giving it to a Log function. Otherwise, you might need to check for unescaped quotes causing issues inside your string.