Why show java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String?

不想你离开。 提交于 2019-11-30 11:48:29

SpannableString is not String directly. so, you can not cast. but, it can be converted to string. you can convert something to string with concatenating with empty string.

pasteData = "" + item.getText();

From CharSequence.toString()

Returns a string with the same characters in the same order as in this sequence.

You need to use next code.

String pasteData = item.getText().toString();

You can not cast to android.text.SpannableString because item.getText() returns CharSequence, there are a lot of implementations of it

If your Spanned text only containing HTML content then you can convert it using Html.toHtml()

String htmlString = Html.toHtml(spannedText);

It has worked for me String htmlString = String.valueOf(Html.fromHtml(contenttext,Html.FROM_HTML_MODE_COMPACT));

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!