[英]unchecked cast Map <String, String> allPreferences
I have a gradle warning about unchecked cast. 我有有关未检查的演员表的警告。 The app builds without bugs but I want to clean up any code issues.
该应用程序的构建没有错误,但是我想清理所有代码问题。 I saw a similar post here How do I cast from int to generic type Integer?
我在这里看到过类似的帖子如何将int转换为泛型Integer?
It is not clear to me how to check the value for the cast. 我不清楚如何检查演员表的价值。
.../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 ?
line 56 Map<String, String> allPreferences = (Map<String, String>) sharedPreferences.getAll();
第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));
}
请注意, SharedPreferences
可以包含除字符串之外的其他对象。如果您100%确保SharedPreferences
对象中只有Strings,则可以忽略此警告,并在方法中或转换的上方添加@SuppressWarnings("unchecked")
。
getAll returns a Map<String, ?>
not a Map<String, String>
, where the unbounded wildcard ?
getAll返回
Map<String, ?>
而不是Map<String, String>
,其中无界通配符?
, is a short version of <? extends Object>
,是
<? extends Object>
的缩写<? extends Object>
<? extends Object>
. <? extends Object>
。 If I didn't misunderstood you 如果我没误会你
Map<String, ?> allPreferences = sharedPreferences.getAll();
should remove the warning 应该删除警告
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.