java.lang.ClassCastException:android.text.SpannableStringBuilder不能转换为java.util.ArrayList中 [英] java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.util.ArrayList
问题描述
我使用PhoneGap的Android插件:EmailComposerwithAttachments <一个href=\"https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments\">https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments它在执行startActivitywithResult功能时,会出现以下错误。我使用的是Android 4.2版本2.5.0科尔多瓦
java.lang.ClassCastException:android.text.SpannableStringBuilder不能转换到的java.util.ArrayList
//设置附件
尝试{
JSONArray附件= parameters.getJSONArray(附件);
如果(附件=空&放大器;!&放大器; attachments.length()大于0){
ArrayList的&LT;&乌里GT;的URI =新的ArrayList&LT;&乌里GT;();
//从路径转换到Android友好Parcelable的URI
的for(int i = 0; I&LT; attachments.length();我++){
尝试{
档案文件=新的文件(attachments.getString(I));
如果(file.exists()){
URI URI = Uri.fromFile(文件);
uris.add(URI);
}
}赶上(例外五){
LOG.e(EmailComposer,错误添加附件:+ e.toString());
}
}
如果(uris.size()大于0){
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,URI的);
}
}
}赶上(例外五){
LOG.e(EmailComposer,错误处理附件参数:+ e.toString());
} this.cordova.startActivityForResult(此,emailIntent,0);
以下是完整的曲线。
04-02 16:34:13.120:W /包(698):主要android.intent.extra.TEXT预期的ArrayList&LT;&CharSequence的GT;但值是一个android.text.SpannableStringBuilder。默认值为&LT;&空GT;被退回。04-02 16:34:13.150:W /包(698):尝试投产生内部异常:04-02 16:34:13.150:W /包(698):java.lang.ClassCastException:
android.text.SpannableStringBuilder不能转换为java.util.ArrayList中04-02 16:34:13.150:W /包(698):在android.os.Bundle.getCharSequenceArrayList(Bundle.java:1326)04-02 16:34:13.150:W /包(698):在android.content.Intent.getCharSequenceArrayListExtra(Intent.java:4224)04-02 16:34:13.150:W /包(698):在android.content.Intent.migrateExtraStreamToClipData(Intent.java:6682)04-02 16:34:13.150:W /包(698):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1410)04-02 16:34:13.150:W /包(698):在android.app.Activity.startActivityForResult(Activity.java:3351)04-02 16:34:13.150:W /包(698):在android.app.Activity.startActivityForResult(Activity.java:3312)04-02 16:34:13.150:W /包(698):在org.apache.cordova.DroidGap.startActivityForResult(DroidGap.java:812)04-02 16:34:13.150:W /包(698):在org.apache.cordova.example.EmailComposer.sendEmail(EmailComposer.java:157)04-02 16:34:13.150:W /包(698):在org.apache.cordova.example.EmailComposer.execute(EmailComposer.java:36)04-02 16:34:13.150:W /包(698):在org.apache.cordova.api.CordovaPlugin.execute(CordovaPlugin.java:61)04-02 16:34:13.150:W /包(698):在org.apache.cordova.api.PluginManager.exec(PluginManager.java:220)04-02 16:34:13.150:W /包(698):在org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:44)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewCore.nativeMouseClick(本机方法)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewCore.nativeMouseClick(本机方法)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewCore.access $ 6800(WebViewCore.java:57)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewCore $ EventHub.dispatchWebKitEvent(WebViewCore.java:1806)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewInputDispatcher.dispatchWebKitEvent(WebViewInputDispatcher.java:689)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewInputDispatcher.dispatchWebKitEvents(WebViewInputDispatcher.java:639)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewInputDispatcher.access $ 800(WebViewInputDispatcher.java:78)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewInputDispatcher $ WebKitHandler.handleMessage(WebViewInputDispatcher.java:1153)04-02 16:34:13.150:W /包(698):在android.os.Handler.dispatchMessage(Handler.java:99)04-02 16:34:13.150:W /包(698):在android.os.Looper.loop(Looper.java:137)04-02 16:34:13.150:W /包(698):在android.webkit.WebViewCore $ WebCoreThread.run(WebViewCore.java:827)04-02 16:34:13.150:W /包(698):在java.lang.Thread.run(Thread.java:856)
这下这个的 Android中4.x的这个bug
您可以通过替换EmailComposer.java此行解决纯文本电子邮件的问题:
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,体);
与
的ArrayList&LT;串GT; extra_text =新的ArrayList&LT;串GT;();
extra_text.add(体);
emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT,extra_text);
但是,这不会对HTML邮件的工作,因为跨区(由Html.fromHtml返回)不是为CharSequence的子类。当我试图Html.fromHtml()的结果转换为字符串,标签出现作为文本的一部分: - (
此外,当我尝试这样做,纯文本邮件正文中使用Gmail应用程序时出现,但它并没有出现在股票电子邮件应用程序 - 身体总是空白
。I'm using the phonegap Android plugin: EmailComposerwithAttachments https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments and it occurs the following error when executing startActivitywithResult function. I'm using Android 4.2 with Cordova 2.5.0
java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.util.ArrayList
// setting attachments
try {
JSONArray attachments = parameters.getJSONArray("attachments");
if (attachments != null && attachments.length() > 0) {
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (int i=0; i<attachments.length(); i++) {
try {
File file = new File(attachments.getString(i));
if (file.exists()) {
Uri uri = Uri.fromFile(file);
uris.add(uri);
}
} catch (Exception e) {
LOG.e("EmailComposer", "Error adding an attachment: " + e.toString());
}
}
if (uris.size() > 0) {
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
}
}
} catch (Exception e) {
LOG.e("EmailComposer", "Error handling attachments param: " + e.toString());
}
this.cordova.startActivityForResult(this, emailIntent, 0);
The following is the complete trace.
04-02 16:34:13.120: W/Bundle(698): Key android.intent.extra.TEXT expected ArrayList<CharSequence> but value was a android.text.SpannableStringBuilder. The default value <null> was returned.
04-02 16:34:13.150: W/Bundle(698): Attempt to cast generated internal exception:
04-02 16:34:13.150: W/Bundle(698): java.lang.ClassCastException:
android.text.SpannableStringBuilder cannot be cast to java.util.ArrayList
04-02 16:34:13.150: W/Bundle(698): at android.os.Bundle.getCharSequenceArrayList(Bundle.java:1326)
04-02 16:34:13.150: W/Bundle(698): at android.content.Intent.getCharSequenceArrayListExtra(Intent.java:4224)
04-02 16:34:13.150: W/Bundle(698): at android.content.Intent.migrateExtraStreamToClipData(Intent.java:6682)
04-02 16:34:13.150: W/Bundle(698): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1410)
04-02 16:34:13.150: W/Bundle(698): at android.app.Activity.startActivityForResult(Activity.java:3351)
04-02 16:34:13.150: W/Bundle(698): at android.app.Activity.startActivityForResult(Activity.java:3312)
04-02 16:34:13.150: W/Bundle(698): at org.apache.cordova.DroidGap.startActivityForResult(DroidGap.java:812)
04-02 16:34:13.150: W/Bundle(698): at org.apache.cordova.example.EmailComposer.sendEmail(EmailComposer.java:157)
04-02 16:34:13.150: W/Bundle(698): at org.apache.cordova.example.EmailComposer.execute(EmailComposer.java:36)
04-02 16:34:13.150: W/Bundle(698): at org.apache.cordova.api.CordovaPlugin.execute(CordovaPlugin.java:61)
04-02 16:34:13.150: W/Bundle(698): at org.apache.cordova.api.PluginManager.exec(PluginManager.java:220)
04-02 16:34:13.150: W/Bundle(698): at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:44)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewCore.nativeMouseClick(Native Method)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewCore.nativeMouseClick(Native Method)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewCore.access$6800(WebViewCore.java:57)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewCore$EventHub.dispatchWebKitEvent(WebViewCore.java:1806)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewInputDispatcher.dispatchWebKitEvent(WebViewInputDispatcher.java:689)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewInputDispatcher.dispatchWebKitEvents(WebViewInputDispatcher.java:639)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewInputDispatcher.access$800(WebViewInputDispatcher.java:78)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewInputDispatcher$WebKitHandler.handleMessage(WebViewInputDispatcher.java:1153)
04-02 16:34:13.150: W/Bundle(698): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 16:34:13.150: W/Bundle(698): at android.os.Looper.loop(Looper.java:137)
04-02 16:34:13.150: W/Bundle(698): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:827)
04-02 16:34:13.150: W/Bundle(698): at java.lang.Thread.run(Thread.java:856)
It's down this this bug in Android 4.x
You can work around the problem for plain text emails by replacing this line in EmailComposer.java:
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
with
ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add(body);
emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
But this won't work for HTML emails because Spanned (returned by Html.fromHtml) is not a subclass of Charsequence. When I tried casting the result of Html.fromHtml() to a string, the tags appeared as part of the text :-(
Also when I tried this, the body of plain text emails to appeared when using the Gmail app but it didn't appear in stock Email app - body was always blank.
这篇关于java.lang.ClassCastException:android.text.SpannableStringBuilder不能转换为java.util.ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!