Home > Enterprise >  string.atChar(0) crashes
string.atChar(0) crashes

Time:02-24

I have an array koal of strings with "00" at koal[0] but this attempt to get 1st element from first string crashes:

tv6.setText(koal[0].charAt(0));

On the other hand: tv6.setText(koal[0]) prints "00".

Why? Or, how do I get ith element from koal[0] ?

CodePudding user response:

charAt() returns a char, which is a numeric type that is then coerced to an int, meaning that you're accidentally calling the overload of setText() that expects to be passed an R.string resource id.

If you want to use the overload of setText() that accepts a string, you'll have to manually convert that char to a string somehow. One very easy way is to use string concatenation:

tv6.setText(""   koal[0].charAt(0));

Of course, there are many ways to convert char to String; this is just the easiest.

  • Related