-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
  • 8,346
  • 16
  • 39
  • 53
Vivank
  • 156
  • 1
  • 14
  • 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,891
  • 2
  • 25
  • 43
0

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

String.valueOf(integer)

And thus will work.

Prasheel
  • 985
  • 5
  • 22