Java.lang.ClassCastException : android. text. SpannableString cannot be converted to java.lang.String

Java.lang.ClassCastException : android. text. SpannableString cannot be converted to java.lang.String … here is a solution to the problem.

Java.lang.ClassCastException : android. text. SpannableString cannot be converted to java.lang.String

This will be displayed in a monospace font. The first four spaces
will be stripped, but all other white space will be retained.

String letterStr = null;
letterStr = (String)((TextView)view).getText();

Solution

String contains html markup, which Android treats as Spannable.

You can assign the return value of getText() to a CharSequence object, which is the parent class of String and Spannable, or replace type conversions with toString().

letterStr = ((TextView)view).getText().toString();

The former will keep the html markup, the latter will not

Related Problems and Solutions