Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Lists and Adapters (2015) Acting on List Item Taps Wrapping Up

Stormy Weather App: Crashing & Context

Crashing: When the user starts up the app and no available network is detected, a Toast message is displayed. However, when the user taps on the Hourly or Weekly buttons, the app crashes. This issue can be easily fixed with a simple if statement to check if the network is available upon button tap, and if not, an else statement can execute a Toast message notifying the user about the lack of network connection.

    @OnClick (R.id.dailyButton)
    public void startDailyActivity(View view) {
        if (isNetworkAvailable()) {
            Intent intent = new Intent(this, DailyForecastActivity.class);
            intent.putExtra(DAILY_FORECAST, mForecast.getDailyForecast());
            startActivity(intent);
        }
        else {
            Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
        }
    }

    @OnClick (R.id.hourlyButton)
    public void startHourlyActivity(View view) {
        if (isNetworkAvailable()) {
            Intent intent = new Intent(this, HourlyForecastActivity.class);
            intent.putExtra(HOURLY_FORECAST, mForecast.getHourlyForecast());
            startActivity(intent);
        } else {
            Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
        }
    }

Context: There is a small discrepancy in ViewGroup's variable name in the HourViewHolder method located within the HourAdapter class. I believe this is due to varying Android Studio versions. Another member provided a solution.