Whether I try to change a textView or a button, why does Android Studio require that I pass only chars and not strings in the setText() method?
Asked
Active
Viewed 98 times
-2
-
Pretty sure Strings are `CharSequence`s; or at least coercable to `CharSequence`s. Did you try? – Carcigenicate Dec 26 '16 at 21:31
-
Yes, it works to recast the char sequences into strings. – johannes saxon Dec 27 '16 at 15:49
-
But how does one pass a string directly? – johannes saxon Dec 27 '16 at 15:52
-
It should work to pass a string directly. – Carcigenicate Dec 27 '16 at 15:52
1 Answers
0
String
s are automatically converted to CharSequence
s when necessary (or are CharSequence
s; can't remember. The point is, they're compatible):
public class Main {
public static void testF(CharSequence seq) {
System.out.println(seq);
}
public static void main(String[] args) {
testF("Hello"); // Prints "Hello"
}
}
This would have been a good thing to try on your own or search for first.

Carcigenicate
- 43,494
- 9
- 68
- 117