处理多联系人按国家编辑时遇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
相关产品推荐
相关产品推荐

