gpt4 book ai didi

android - 类型不匹配 : inferred type is String but Charset was expected in kotlin

转载 作者:IT老高 更新时间:2023-10-28 13:40:58 24 4
gpt4 key购买 nike

我的主要 Activity 中有以下代码:

var qNa_list = parseQuestions(loadJSONFromAsset("qna_list.json"))


fun loadJSONFromAsset(file_name:String): String? {
var json: String? = null
try {

val isis = assets.open(file_name)

val size = isis.available()

val buffer = ByteArray(size)

isis.read(buffer)

isis.close()

json = String(buffer, "UTF-8")


} catch (ex: IOException) {
ex.printStackTrace()
return null
}

return json

}

当我尝试编译它时,我收到以下错误。

我修复了一些其他由 nullables 引起的错误,但这个是我无法解码的。

错误:(127, 35) 类型不匹配:推断类型为 String 但预期为 Charset

我已将一些值更改为可为空以适应错误,但 json = String(buffer, "UTF-8") (UTF-8) 始终带有红色下划线。

最佳答案

这似乎解决了问题。

看来我需要指定 Charset 类型的对象,而不是像 UTF-8 这样的字符串。

@Maroš Šeleng 提到的第一种方法

Charset.forName("UTF-8")

或者,指定 Charset.UTF_8

val charset: Charset = Charsets.UTF_8

json = String(buffer, charset)

关于android - 类型不匹配 : inferred type is String but Charset was expected in kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47198556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com