udacity / Sunshine-Version-2

The official repository for Developing Android Apps
Apache License 2.0
2.87k stars 4.41k forks source link

Adding Images to the App Resources$NotFoundException Resource ID #0xffffffff #326

Closed francisrod01 closed 7 years ago

francisrod01 commented 7 years ago

Hi, in my lesson Adding Images to the App on ud853 course I had a problem below.

In my case, this art function receives weatherId of DetailsFragment with value 122, 263, 356 and run a bug, because this values aren't in getArtResourceForWeatherCondition() method.

I receive weatherId with value 122 when I click on item list and redirected to DetailFragment with a bug.

https://github.com/udacity/Sunshine-Version-2/compare/5.07_details_screen...5.08_images#diff-7537c0d5330829deab3c57d11a53aa5dR219

screenshot_1490238201 screenshot_1490238257 screenshot_1490238267

When I click in another item:

image

FATAL EXCEPTION: main
Process: com.example.android.sunshine.app, PID: 6824
android.content.res.Resources$NotFoundException: Resource ID #0xffffffff at android.content.res.Resources.getValue(Resources.java:1266)

Code:

// DetailFragment

@Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.v(LOG_TAG, "In onLoadFinished");
        if (!data.moveToFirst()) {
            return;
        }

        // Read weather condition ID from cursor.
        int weatherId = data.getInt(COL_WEATHER_CONDITION_ID);

        // Use weather art image.
        mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));

...

Utility.java


/**
     * Helper method to provide the art resource id according to the weather condition id returned
     * by the OpenWeatherMap call.
     *
     * @param weatherId from OpenWeatherMap API response.
     * @return resource id for the corresponding icon. -1 if no relation is found.
     */
    public static int getArtResourceForWeatherCondition(int weatherId) {
        // Based on weather code data found at:
        // https://openweathermap.org/weather-conditions
        if (weatherId >= 200 && weatherId <= 232) {
            return R.drawable.art_storm;
        } else if (weatherId >= 300 && weatherId <= 321) {
            return R.drawable.art_light_rain;
        } else if (weatherId >= 500 && weatherId <= 504) {
            return R.drawable.art_rain;
        } else if (weatherId == 511) {
            return R.drawable.art_snow;
        } else if (weatherId >= 520 && weatherId <= 531) {
            return R.drawable.art_rain;
        } else if (weatherId >= 600 && weatherId <= 622) {
            return R.drawable.art_snow;
        } else if (weatherId >= 701 && weatherId <= 761) {
            return R.drawable.art_fog;
        } else if (weatherId == 761 || weatherId == 781) {
            return R.drawable.art_storm;
        } else if (weatherId == 800) {
            return R.drawable.art_clear;
        } else if (weatherId == 801) {
            return R.drawable.art_light_clouds;
        } else if (weatherId >= 802 && weatherId <= 804) {
            return R.drawable.art_clouds;
        }
        return -1;
    }
francisrod01 commented 7 years ago

I apologize for my carelessness when duplicating the value of the weather_condition_id index. It's all right now.