Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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
Java 为什么在setText中使用整数值时程序崩溃?_Java_Android - Fatal编程技术网

Java 为什么在setText中使用整数值时程序崩溃?

Java 为什么在setText中使用整数值时程序崩溃?,java,android,Java,Android,我正在尝试设置按钮中的文本 answers是整数的ArrayList 将其转换为string时,效果非常理想 button0.setText(Integer.toString(answers.get(0))); 但在使用整数值时,我的应用程序崩溃了 button0.setText(answers.get(0)); 有什么建议可以解释为什么会发生这种情况吗 作为TextView.seText()可以轻松地将整数文本设置为它,但为什么按钮无法执行此操作?发生这种情况是因为setText()需

我正在尝试设置按钮中的文本

answers是整数的
ArrayList

将其转换为
string
时,效果非常理想

button0.setText(Integer.toString(answers.get(0))); 
但在使用整数值时,我的应用程序崩溃了

button0.setText(answers.get(0)); 
有什么建议可以解释为什么会发生这种情况吗


作为
TextView.seText()
可以轻松地将整数文本设置为它,但为什么按钮无法执行此操作?

发生这种情况是因为setText()需要一个字符串作为参数。就这样做吧

String.valueOf(integer)

这是因为setText()只需要字符串或字符[]

因此,您可以执行类型转换,也可以添加带有数字的引号

  • 按类型转换字符串。值(编号)
  • 通过添加带有数字quantityTextView.setText(“+number”)的“”;或 quantityTextView.setText(数字+“”)
  • setText(Integer.toString(number))

请参见

崩溃?你确定你说的不是编译器错误吗?提示:阅读并相应增强您的问题。因为整数文本(数字
0
的ASCII表示形式)和整数零之间存在差异。数字不是字符。它们是数字,这就是为什么它们被称为整数而不是字符或字符串。