问题描述
在任何浏览器页面复制串然后pasteData正常工作。但是,当副本发送邮件项目编辑器(场),然后应用程序崩溃,并显示SpannedString消息
When copy String from any browser page then pasteData works properly.but when copy SpannedString from message sent item editor(field) then application crash and showthe message
java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
我的code:
My code:
// since the clipboard contains plain text.
ClipData.Item item = clipBoard.getPrimaryClip().getItemAt(0);
// Gets the clipboard as text.
String pasteData = new String();
pasteData = (String) item.getText();
在这里ClipboardManager实例剪贴板来自以下codeS
where ClipboardManager instance is clipBoard come from below codes
clipBoard = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
clipBoard.addPrimaryClipChangedListener(new ClipboardListener());
什么办法,我想在字符串甲终于pasteData。它是如何消除任何碰撞,并在字符串或 SpannableString
这两个工作情况。如果上述code,建议,意见,或有帮助教程AP preciate与亲切的任何变化。
Any way, i want finally pasteData in String formate.How it is remove any crash and also work both situation in String or SpannableString
.If any change above code,suggestion,comment, or helpful tutorial appreciate with cordially.
推荐答案
SpannableString不是字符串直接。所以,你可以不投。但是,它可以被转换成字符串。你可以转换的东西字符串与空字符串串联。
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();
这篇关于为什么显示java.lang.ClassCastException:android.text.SpannableString不能转换为java.lang.String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!