You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

处理多联系人按国家编辑时遇Interface Entry无构造函数警告

解决Map.Entry遍历的"Interface Entry does not have constructors"警告

你的循环写法存在语法错误,重复使用in且错误声明了Map.Entry的类型——Map.Entry是接口,不能直接实例化,这正是警告的来源。

修正后的代码

直接用Kotlin简化的Map遍历语法即可:

for ((key, value) in dialCodes) {
    val code = key.uppercase()
    val countryName = Locale("", code).displayCountry
    if (TextUtils.isEmpty(countryName)) {
        Log.w(TAG, "Country name missing for '$code'")
    }
    for (prefix in value) {
        countryList.add(CountryCode(code, countryName, prefix.trim { it <= ' ' }))
    }
    countryList.sort() // 替换Collections.sort,更符合Kotlin风格
}

补充说明

如果需要显式操作Entry对象,也可以这样写:

for (entry in dialCodes.entries) {
    val key = entry.key
    val value = entry.value
    // 后续逻辑和上面一致
}

两种写法都能避免你遇到的警告,核心是不要错误地在循环里声明Map.Entry<String, Array<String>>的类型,Kotlin会自动推断类型。

内容的提问来源于stack exchange,提问作者Mahmoud Nabil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:02:04