unchecked cast Map <string,string =“”> allPreferences </string,>

时间:2015-03-01 19:02:30

标签: java android casting gradle

我有关于未经检查的演员的gradle警告。该应用程序构建没有错误,但我想清理任何代码问题。我在这里看到了类似的帖子How do I cast from int to generic type Integer?

我不清楚如何检查演员的价值。

.../mayday/md/model/SMSSettings.java:56: warning: [unchecked] unchecked cast
        Map<String, String> allPreferences = (Map<String, String>) sharedPreferences.getAll();
                                                                                           ^
  required: Map<String,String>
  found:    Map<String,CAP#1>
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Object from capture of ?

第56行Map<String, String> allPreferences = (Map<String, String>) sharedPreferences.getAll();

public static SMSSettings retrieve(Context context) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    Map<String, String> allPreferences = (Map<String, String>) sharedPreferences.getAll();
    List<String> retrievedPhoneNumbers = retrievePhoneNumbers(allPreferences);
    return new SMSSettings(retrievedPhoneNumbers, allPreferences.get(SMS_MESSAGE));
}

2 个答案:

答案 0 :(得分:1)

注意,SharedPreferences can contain different objects than just Strings 如果您100%确定在SharedPreferences对象中只有字符串,则可以忽略此警告,并在您的方法或上面的行中添加@SuppressWarnings("unchecked")

答案 1 :(得分:0)

getAll返回Map<String, ?>而不是Map<String, String>,其中无界通配符?<? extends Object>的简短版本。如果我没有误解你

 Map<String, ?> allPreferences = sharedPreferences.getAll();

应删除警告