Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android toInt()返回ASCII值_Android_Kotlin - Fatal编程技术网

Android toInt()返回ASCII值

Android toInt()返回ASCII值,android,kotlin,Android,Kotlin,我正在研制一个三轨分步测序器,用于样本选择的微调器给我带来了一些麻烦。加载保存的模式以设置正确的样本时,将执行以下代码 for ((index, j) in spinners.withIndex()){ val item = loadingPattern[(48+index)] Log.i("item", item.toString()) Log.i("int", item.toInt().toString) j.setSelection(item.toInt()) }

我正在研制一个三轨分步测序器,用于样本选择的微调器给我带来了一些麻烦。加载保存的模式以设置正确的样本时,将执行以下代码

for ((index, j) in spinners.withIndex()){
   val item = loadingPattern[(48+index)]
   Log.i("item", item.toString())
   Log.i("int", item.toInt().toString)
   j.setSelection(item.toInt())
}
数组“微调器”包含3个微调器,每个微调器有5个条目。“loadingPattern”是包含模式信息的字符串。最后三个字符是与微调器位置相对应的整数。当微调器选择如下所示时:

微调器1:所选项目索引0 微调器2:所选项目索引3 微调器3:选定项索引4

日志“item”精确打印这些值。然而,日志“int”打印48+索引,因此在本例中为48,50,52。由于toInt()函数也会对解析为.setSelection()函数的值进行调用,因此会触发此异常:

java.lang.ArrayIndexOutOfBoundsException:长度=2;索引=48

如果有人知道为什么会这样,我很乐意知道。非常感谢大家!我爱这个社区

编辑: 如果模式是这样编程的:

“模式字符串”如下所示: 101010101010101010000000100000010000001000420

活动步骤由1表示,其他步骤由0表示。最后3位是微调器位置。也许这会有帮助:)

编辑2: 我刚刚发现了这个问题: 他们说0的ASCII值是48这意味着toInt()可能返回0的ASCII值。但是为什么会这样?我怎么才能得到正整数呢?

我找到了!
如本文所述,toInt()函数仅适用于字符串,并返回字符的ASCII值。正确的方法是使用Character.getNumericValue()。

显示“loadingPattern”字符串。如果
loadingPattern
是字符串,则
loadingPattern[i]
返回一个
Char
。Char是一种数字类型,因此它的
toInt()
方法只执行强制(与调用
Integer.parseInt()
String.toInt()
相反)。