-1

I am trying to set text in my button .

answers is an ArrayList of Integers

On converting it to string it works perfectly

button0.setText(Integer.toString(answers.get(0))); 

But on using integer value my app crashes.

button0.setText(answers.get(0)); 

Any suggestions why this happening ?

As TextView.seText() easily set the integer text to it but why button is unable to do it ?

coder
  • 7,778
  • 15
  • 37
  • 52
Vivank
  • 146
  • 1
  • 13
  • Crashes? Are you sure you are not talking about a compiler error? Hint : read [mcve] and enhance your question accordingly. – GhostCat May 04 '18 at 17:12
  • Because there's a difference between *integer text* (the ASCII representation of the digit `0`) and the *integer* zero. Numbers are not characters. They're numbers, which is why they're called integers and not characters or strings. – Ken White May 04 '18 at 17:39

2 Answers2

1

It is happening because setText() only expects string or char[].

So either you can perform type casting or you can add quotes with the number

  • By type casting String.valueOf(number)
  • By adding "" with the number quantityTextView.setText(""+number); or quantityTextView.setText(number+"");
  • textView.setText(Integer.toString(number));

See https://developer.android.com/reference/android/widget/TextView#setText(int)

Däñish Shärmà
  • 2,853
  • 2
  • 24
  • 41
0

This happens because setText() expects a String as argument. Just do like this

String.valueOf(integer)

And thus will work.

Prasheel
  • 974
  • 5
  • 22