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

用户10866317

这是一个安卓项目。我对 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>

如果您需要其他代码或任何详细信息,请告诉我。谢谢你。

m_katsifarakis

Set是泛型类型。它是一个容器,可以容纳任何类型的对象。

在您的情况下,您的 Set 似乎包含Map.Entry<String, Object>对象,但由于您没有在任何地方指定对象,Java 假定您的 Set 包含Objects (所有其他类派生自的 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android Studio片段不兼容类型错误

如何从Android Studio中不兼容的类型错误

在Android Studio中出现vitamio错误。不兼容的类型

不兼容的类型。在 android 中编辑文本错误

Android Studio 不兼容的类型

整个项目中的 Android 错误

程序类型已经存在:Xamarin.Android项目中的android.support.annotation.PluralsRes错误

Android Fragment Manager不兼容的类型

Android Volley newRequestQueue-不兼容的类型

当尝试在Android Studio中实现tensorflow lite模型时,出现了不兼容的数据类型错误

如何修复错误:类型不兼容:MenuFragment 无法在 android 中转换为 Fragment?

错误:不兼容的类型:org.opencv.core.Point无法转换为android.graphics.Point

Android Studio:错误:不兼容的类型:double无法转换为String

Android Studio:错误:类型不兼容:MainActivity 无法转换为应用程序

Android Retrofit RecyclerView 错误:类型不兼容:<anonymous Callback<ServiceResponse>> 无法转换为 Callback<Service>

Firebase错误:不兼容的Android SDK

在Android项目中启动错误的活动

错误不兼容的类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager

Android Studio 3.0错误:android-apt不兼容

错误:(26)错误:“'与属性android:icon(attr)参考不兼容

不兼容的类型:无法在Android中将HomeFragment转换为Fragment

不兼容的类型:必需T找到T Android Studio

不兼容的类型:FragmentDark无法在Android中转换为Fragment

不兼容的类型-必需:android.net.Uri,发现:int

Java Gremlin union 与项目不兼容类型错误

Android:RecyclerView不兼容

错误:Android HTC设备中的“所选设备不兼容”

Android:gradlew皮棉错误“不兼容的Gradle版本”

视图绑定错误:与属性android:visibility不兼容