【问题标题】:java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.util.ArrayListjava.lang.ClassCastException: android.text.SpannableStringBuilder 不能转换为 java.util.ArrayList
【发布时间】:2013-03-24 11:22:09
【问题描述】:

我正在使用 phonegap Android 插件:EmailComposerwithAttachments https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments 执行 startActivitywithResult 函数时出现以下错误。我正在使用带有 Cordova 2.5.0 的 Android 4.2

java.lang.ClassCastException: android.text.SpannableStringBuilder 无法转换为 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);

以下是完整的跟踪。

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)

【问题讨论】:

  • 某事告诉我您没有显示代码的相关部分。您是否在任何地方设置了额外的 TEXT?
  • yarian,我直接从github.com/phonegap/phonegap-plugins/tree/master/Android/…获取了未修改的代码。
  • 我的意思是它看起来像是在它的 // 设置正文部分炸毁,而不是 // 设置附件。
  • 能不能把你传入的参数加进去?
  • 这是我调用函数的方式。我调试了它,代码直到最后一个函数才抛出异常。 window.plugins.emailComposer.showEmailComposerWithCallback(null,"看这张照片","看看这个:",["example@email.com", "johndoe@email.org"] ,[],[],true,["///mnt/sdcard/MyFolder/resized.png"]);

标签: android cordova spannablestring


【解决方案1】:

在 Android 4.x 中是 this bug

您可以通过替换 EmailComposer.java 中的这一行来解决纯文本电子邮件的问题:

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

与

ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add(body);
emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);

但这不适用于 HTML 电子邮件,因为 Spanned(由 Html.fromHtml 返回)不是 Charsequence 的子类。当我尝试将 Html.fromHtml() 的结果转换为字符串时,标签显示为文本的一部分:-(

当我尝试这个时,使用 Gmail 应用程序时会出现纯文本电子邮件的正文,但它没有出现在股票电子邮件应用程序中 - 正文总是空白。

【讨论】:

  • 您好,我的邮件附件也有同样的问题,请您给点建议
  • 另一个使用 ArrayList 和 putStringArrayListExtra() 的注意事项,ClassCastException 消失了,但我的 Nexus 4 API 19 和 HTC Evo API 16 的 Gmail 应用程序正文中没有出现文本。常规 @ 987654325@ 方法导致异常,但至少出现了正文。 (旁注:我使用的是纯 Android,而不是 PhoneGap。)
【解决方案2】:

尝试不同的方法:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
//emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text

startActivity(Intent.createChooser(emailIntent, "Chooser Title"));

取自这里:Send Email Intent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-20
    • 2023-03-29
    • 2013-07-11
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多