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 an Interactive Story App The Rest of the Story Navigation

Binyamin Friedman
Binyamin Friedman
14,615 Points

Why 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
PodcastActivity.java
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

Nothing 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
Binyamin Friedman
14,615 Points

Thank you! Why does getApplicationContext() not work? Is it because in the code challenge they don't have certain methods?

Yup, it has something to do with the code challenge.

Seth Kroger
Seth Kroger
56,413 Points

The 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.