简体   繁体   English

具有Spanned强制转换的Html.toHtml(ClassCastException) <Android>

[英]Html.toHtml with Spanned cast (ClassCastException) <Android>

Trying to format my output (in a TextView) using HTML. 尝试使用HTML格式化我的输出(在TextView中)。 I need to get the text HTML-formating back from the old output, and add the new text one line above it. 我需要从旧的输出中获取HTML格式的文本,并将新文本添加到其上方一行。

String previous = Html.toHtml((Spanned) chatOutput.getText());
chatOutput.setText(Html.fromHtml(message + "<br>" + previous));

This compiles but gives a java.lang.ClassCastException: java.lang.String on runtime at the toHtml() method. 这样可以编译,但在运行时通过toHtml()方法给出java.lang.ClassCastException:java.lang.String。 I saw several people that suggested that the toHtml() method could be used that way, but maybe I'm missing something. 我看到几个人建议可以使用toHtml()方法,但也许我缺少一些东西。

From documentation for getText() 从getText()的文档中

Return the text the TextView is displaying. If setText() was called with an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast the return value from this method to Spannable or Editable,

So you can cast it, but I would still check with instanceof if this is safe, what if textview was cleared with empty text? 这样就可以进行转换了,但是我仍然会用instanceof检查它是否安全,如果用空文本清除了textview怎么办?

TextView chatOutput = (TextView) findViewById(R.id.textView4);
chatOutput.setText("", BufferType.SPANNABLE);

void updateChatOutput {
    String previous = Html.toHtml((Spannable) chatOutput.getText());
    if (!previous.equals("")) {
        if (previous.substring(0, 13).equals("<p dir=\"ltr\">"))
            previous = previous.substring(13, previous.length()-3); //remove <p dir="ltr"> and </p>
        else
            previous = previous.substring(3, previous.length()-3); //remove <p> and </p>
    }
    chatOutput.setText(Html.fromHtml(message.substring(2, message.length()) + "<br>" +  previous));
}

Figured it out. 弄清楚了。 Changing the cast to Spannable and have used the setText method with the BufferType.SPANNABLE solved the orginal problem. 将强制转换更改为Spannable,并将setText方法与BufferType.SPANNABLE结合使用可解决原始问题。

Then a follow-up issue occurred, weird line braking in the textview. 然后发生了后续问题,在文本视图中出现了奇怪的行制动。 Turned out that html-paragraph code was added somewhere in this process, ugly solution below when manually deleting it. 原来,在此过程中某处添加了html段代码,以下为手动删除时的丑陋解决方案。 The solution got even more tacky when I realised that additonal parameters in the paragraph-statement was added by a device with API 18, compared my testing device with API 8. 当我意识到段落声明中的附加参数是由具有API 18的设备添加的,并将我的测试设备与API 8进行了比较时,该解决方案变得更加棘手。

Same problem. 同样的问题。 Had to create new SpannableString before casting to Spanned. 在转换为Spanned之前必须创建新的SpannableString。

Spanned spannedText = new SpannableString(textView.getText());
String htmlString = Html.toHtml(spannedText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 HTML.toHTML() 中使用字符串变量? - how to use string variable in HTML.toHTML()? android:ClassCastException尝试投射片段时 - android: ClassCastException when trying to cast Fragments 返回跨越字符串的android - Return spanned string android 无法将java.lang.ClassCastException强制转换为android.app.Fragment - java.lang.ClassCastException cannot be cast to android.app.Fragment java.lang.ClassCastException:无法投射android.app.Application - java.lang.ClassCastException: android.app.Application cannot be cast Android为跨区文本添加填充 - Android add padding for spanned text 为什么会引发ClassCastException:char []无法转换为android.app.SharedPreferencesImpl - Why it throws ClassCastException: char[] cannot be cast to android.app.SharedPreferencesImpl classcastexception:无法将Fragment_main强制转换为片段活动android - classcastexception: Fragment_main cannot be cast to fragment activity android Android中的Animate Drawable图标-ClassCastException VectorDrawable无法转换为Animatable - Animate Drawable icon in Android - ClassCastException VectorDrawable cannot be cast to Animatable java Android Spanned Html.fromHtml(stringWithCDATA)仍将标签显示为文本 - java Android Spanned Html.fromHtml(stringWithCDATA) still shows tags as text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM