这是一个安卓项目。我对 Java 完全陌生(刚开始学习)。如标题所述,我收到了不兼容的类型错误
我在这里附上了相应的方法:
public void init(Map map) {
this.productIds = new ArrayList();
try {
if (map.containsKey("products")) {
for (Entry<String, Object> "//Error Lies here" entry : ((HashMap) map.get("products")).entrySet()) {
InAppProduct productId = new InAppProduct();
productId.productId = ((String) entry.getKey()).toLowerCase();
HashMap<String, Object> extraValues = (HashMap) entry.getValue();
if (extraValues.containsKey(ShareConstants.MEDIA_TYPE)) {
productId.productType = (String) extraValues.get(ShareConstants.MEDIA_TYPE);
}
if (extraValues.containsKey("days")) {
productId.days = ((Integer) extraValues.get("days")).intValue();
}
this.productIds.add(productId);
}
return;
}
this.productIds = new ArrayList(ConfigurationFetcher.this.mDefaultsDelegate.getDefaultsInAppPackages());
} catch (Exception e) {
e.printStackTrace();
}
}
错误是:
需要
Object
但已找到Entry <String, Object>
如果您需要其他代码或任何详细信息,请告诉我。谢谢你。
Set
是泛型类型。它是一个容器,可以容纳任何类型的对象。
在您的情况下,您的 Set 似乎包含Map.Entry<String, Object>
对象,但由于您没有在任何地方指定对象,Java 假定您的 Set 包含Object
s (所有其他类派生自的 Java 类)并产生Incompatible Type Error。
这是您的代码的一个稍微改变的版本,应该可以工作。
public void init(Map map) {
this.productIds = new ArrayList();
try {
if (map.containsKey("products")) {
// ***** We now specify the type of object that the Set contains.
Set<Map.Entry<String, Object>> entrySet = ((HashMap) hm.get("products")).entrySet();
for (Entry<String, Object> entry : entrySet) {
InAppProduct productId = new InAppProduct();
productId.productId = ((String) entry.getKey()).toLowerCase();
HashMap<String, Object> extraValues = (HashMap) entry.getValue();
if (extraValues.containsKey(ShareConstants.MEDIA_TYPE)) {
productId.productType = (String) extraValues.get(ShareConstants.MEDIA_TYPE);
}
if (extraValues.containsKey("days")) {
productId.days = ((Integer) extraValues.get("days")).intValue();
}
this.productIds.add(productId);
}
return;
}
this.productIds = new ArrayList(ConfigurationFetcher.this.mDefaultsDelegate.getDefaultsInAppPackages());
} catch (Exception e) {
e.printStackTrace();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句