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 trialBinyamin Friedman
14,615 PointsWhy doesn't this work?
./PodcastActivity.java:13: error: cannot find symbol
Toast.makeText(getApplicationContext(), "You pressed the back button!", Toast.LENGTH_SHORT).show();
^
symbol: method getApplicationContext()
location: class PodcastActivity
1 error
import android.os.Bundle;
public class PodcastActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_podcast);
}
@Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "You pressed the back button!", Toast.LENGTH_SHORT).show();
super.onBackPressed();
}
}
1 Answer
Tarek M.Tolba
Courses Plus Student 3,613 PointsNothing wrong with your code, otherwise they are expecting "this" instead of getApplicationContext()
Toast.makeText(this, "You pressed the back button!", Toast.LENGTH_SHORT).show();
super.onBackPressed();
Binyamin Friedman
14,615 PointsBinyamin Friedman
14,615 PointsThank you! Why does getApplicationContext() not work? Is it because in the code challenge they don't have certain methods?
Tarek M.Tolba
Courses Plus Student 3,613 PointsTarek M.Tolba
Courses Plus Student 3,613 PointsYup, it has something to do with the code challenge.
Seth Kroger
56,413 PointsSeth Kroger
56,413 PointsThe challenges aren't tested with the actual Android OS. That would take too many resources. Instead they are tested with mock versions of classes like Activity and Toast that have limited responses that are only those useful for the challenge.
The Android testing course covers the topic of mocking, since it's important to writing automated tests.