为什么Android Studio Lint在使用TypedArray时会发出警告?

时间:2018-03-15 20:11:31

标签: android android-studio lint typedarray

我有这段代码:

TypedArray mAllowedCountries = application.getResources().obtainTypedArray(R.array.allowed_countries);

    for (int index = 0; index < mAllowedCountries.length(); index++) {
        if(mAllowedCountries.getString(index) != null) {
            if (mAllowedCountries.getString(index).toUpperCase().equals(addressComponentCountry.short_name.toUpperCase())) {
                supported = true;
                break;
            }
        }
    }

我收到关于

Android Studio Lint 警告
  

方法调用'toUpperCase'可能会产生   '显示java.lang.NullPointerException'

如何修复此问题以摆脱 Lint 警告?

1 个答案:

答案 0 :(得分:1)

以下内容应删除 Lint 警告:

final String address = addressComponentCountry.short_name.toUpperCase();
String tempStr;
for (int index = 0; index < mAllowedCountries.length(); index++) {
    tempStr = mAllowedCountries.getString(index);
    if(tempStr != null && tempStr.toUpperCase().equals(address)) {
        supported = true;
        break;
    }
}