简体   繁体   中英

Incompatible Type Error in Android Project

This is an Android project. I'm completely new to Java (just started learning). 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>

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> .

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. 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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