简体   繁体   English

Android项目中的不兼容类型错误

[英]Incompatible Type Error in Android Project

This is an Android project. 这是一个Android项目。 I'm completely new to Java (just started learning). 我对Java完全陌生(刚刚开始学习)。 As stated in the title, I'm getting an Incompatible Type Error 如标题所述,我收到了不兼容的类型错误

I've attached the respective method here : 我在这里附加了相应的方法:

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();
    }
}

The Error is : 错误是:

Required Object but found Entry <String, Object> 必需的Object但找到Entry <String, Object>

Let me know if you need additional code or any details. 让我知道您是否需要其他代码或任何详细信息。 Thank You. 谢谢。

map.get("products")).entrySet() is a set of products, each product is a Object , not Entry <String, Object> . map.get("products")).entrySet()是一组产品,每个产品都是Object ,而不是Entry <String, Object>

This should work: 这应该工作:

public void init(Map map) {
    this.productIds = new ArrayList();
    try {
        if (map.containsKey("products")) {
            for (Object 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();
    }
}

Set is a generic type. Set是泛型类型。 It is a container that can contain any kind of object. 它是一个可以包含任何对象的容器。

In your case, it seems that your Set contains Map.Entry<String, Object> objects but since you don't specify that anywhere, Java assumes your Set contains Object s (the Java class that all other classes derive from) and produces an Incompatible Type Error . 在您的情况下,您的Set似乎包含Map.Entry<String, Object>对象,但是由于您未在任何地方指定,因此Java假定您的Set包含Object (所有其他类派生的Java类)并产生一个不兼容的类型错误

Here's a slightly altered version of your code that should work. 这是您的代码应稍作改动的版本。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM