为什么显示java.lang.ClassCastException:android.text.SpannableString不能转换为java.lang.String?转换为、ClassCastExc

由网友(思念是毒薬)分享简介:在任何浏览器页面复制串然后pasteData正常工作。但是,当副本发送邮件项目编辑器(场),然后应用程序崩溃,并显示SpannedString消息When copy String from any browser page then pasteData works properly.but when copy...

在任何浏览器页面复制串然后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 show the 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();
阅读全文

相关推荐

最新文章