Java 为什么android中的setText在只传递int时出错?
下面是我的代码Java 为什么android中的setText在只传递int时出错?,java,android,algorithm,android-studio,Java,Android,Algorithm,Android Studio,下面是我的代码 public void display(int number){ TextView quantity = findViewById(R.id.quantity); quantity.setText(number); } 当我给出setText(number)时,它会给出一个错误,表明找不到任何资源。 但是当我给出setText(“+number”) 然后它就完美地工作了。 我缺少一些基础知识吗?试试这个 public void disp
public void display(int number){
TextView quantity = findViewById(R.id.quantity);
quantity.setText(number);
}
当我给出setText(number)
时,它会给出一个错误,表明找不到任何资源。
但是当我给出setText(“+number”)代码>
然后它就完美地工作了。
我缺少一些基础知识吗?试试这个
public void display(int number){
TextView quantity = findViewById(R.id.quantity);
quantity.setText(String.valueOf(number));
}
setText()
只接受String
值,而int number
是一个数字,因此我们将其转换为String
好的,谢谢你,我收到了!!为什么投票决定重新开放?链接的副本非常合适,上面的答案准确地解释了这个问题。