ResourceNotFoundException - 尝试根据String值获取图标

时间:2016-08-09 15:49:56

标签: java android android-layout android-viewholder

我正在使用网络服务从api.forecast.io获取天气列表。当我得到参数字符串图标时,我正在从api解析字符串图标值并从具有viewholder的类实用程序中获取图标资源

public static int getIconfromWeatherCond (String icon) {
    if (icon=="clear-day") {
        return R.drawable.ic_clear;
    }
    else if (icon=="clear-night") {
        return R.drawable.ic_clear;
    }
    else if (icon=="rain") {
        return R.drawable.ic_rain;
    }
    else if (icon=="snow") {
        return R.drawable.ic_snow;

    }
    else if (icon=="sleet") {
        //hujan campur es
        return R.drawable.ic_snow;
    }
    else if (icon=="wind") {
        return R.drawable.ic_cloudy;
    }
    else if (icon=="fog") {
        return R.drawable.ic_fog;
    }
    else if (icon=="cloudy") {
        return R.drawable.ic_cloudy;
    }
    else if (icon=="partly-cloudy-day") {
        return R.drawable.ic_light_rain;
    }
    else if (icon=="partly-cloudy-night") {
        return R.drawable.ic_light_rain;
    }
    else if (icon=="hail") {
        return R.drawable.ic_snow;
    }
    else if (icon=="thunderstorm") {
        return R.drawable.ic_storm;
    }
    else if (icon=="tornado") {
        return R.drawable.ic_storm;
    }
    return -1;
}

这是 getIconfromWeatherCond

的方法
config.set({ 
    basePath: "./", 
    frameworks: ["jasmine"], 
    files: [            
        //load files here, including your spec and source files
    ],
    // Other settings...
    browsers: ["PhantomJS"],
    plugins: [
        "karma-jasmine",
        "karma-coverage", //<-- using karma-coverage for code coverage
        "karma-chrome-launcher",
        "karma-phantomjs-launcher"
    ],
    // Coverage reporter generates the coverage
    reporters: ["progress", "coverage"], //<-- reporter for code coverage
    // Source files that you wanna generate coverage for.
    // Do not include tests or libraries (these files will be instrumented by Istanbul)
    preprocessors: {
        "test/bin/js/app/**/!(*spec).js": ["coverage"] //<-- coverage pre-process
    }, 
    // Generate the code coverage report (lots of formats available)
    coverageReporter: {
        reporters:[
            { type: "html", dir: "./test/bin", subdir: "coverage/html" }
        ]
    },
    singleRun: true
})

但是当我试图在我的设备上运行时,我得到这样的错误。 android.content.res.Resources $ NotFoundException:资源ID#0xffffffff 我该怎么办?

1 个答案:

答案 0 :(得分:0)

编辑:

对您的getIconfromWeatherCond方法进行编码,如下所示:

    public static int getIconfromWeatherCond (String icon) {

        switch (icon) {
        case "clear-day":
            return R.drawable.ic_clear;
        case "clear-night":
            return R.drawable.ic_clear;
        case "rain":
            return R.drawable.ic_rain;
        case "snow":
            return R.drawable.ic_snow;
        case "sleet":
            //hujan campur es
            return R.drawable.ic_snow;
        case "wind":
            return R.drawable.ic_cloudy;
        case "fog":
            return R.drawable.ic_fog;
        case "cloudy":
            return R.drawable.ic_cloudy;
        case "partly-cloudy-day":
            return R.drawable.ic_light_rain;
        case "partly-cloudy-night":
            return R.drawable.ic_light_rain;
        case "hail":
            return R.drawable.ic_snow;
        case "thunderstorm":
            return R.drawable.ic_storm;
        case "tornado":
            return R.drawable.ic_storm;
        default:
            return -1;
        }
    }

检查“无图标”值:

  int resId = Utility.getIconfromWeatherCond(item.getIcon());
  if (resId != -1) {
      vh.icon.setImageResource(resId);
  } else {
      Log.d(TAG, "No image resource for icon: " + item.getIcon());
  }

现在你的logcat会告诉你你没有什么价值,但是知道这些远程服务是如何工作的,我敢打赌它只是空白。