Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从包裹中检索使用TextUtils.writeToParcel(…)保存的CharSequence?_Android_Parcelable - Fatal编程技术网

Android 如何从包裹中检索使用TextUtils.writeToParcel(…)保存的CharSequence?

Android 如何从包裹中检索使用TextUtils.writeToParcel(…)保存的CharSequence?,android,parcelable,Android,Parcelable,我希望能够从我的一个应用程序活动向另一个应用程序活动发送格式化文本(即具有格式跨度的文本)。这些CharSequence实例深入到我创建的一些Parcelable类型中 例如,在编组包含格式化的CharSequences的其中一种类型时,我使用TextUtils.writeToParcel,如下所示: public class HoldsCharSequence { /* some formatted string that uses basic spans (e.g., Foregr

我希望能够从我的一个应用程序活动向另一个应用程序活动发送格式化文本(即具有格式跨度的文本)。这些
CharSequence
实例深入到我创建的一些
Parcelable
类型中

例如,在编组包含格式化的
CharSequence
s的其中一种类型时,我使用
TextUtils.writeToParcel
,如下所示:

public class HoldsCharSequence {

    /* some formatted string that uses basic spans (e.g., ForegroundColorSpan) */
    private CharSequence cs;

    public void writeToParcel(Parcel out, int flags) {
        TextUtils.writeToParcel(cs, out, 0);
    }

    /* ... rest of the code ... */
}
问题是我不知道如何从我的私有构造函数中的
中检索
CharSequence

以下操作不起作用:

private HoldsCharSequence(Parcel in) {
    cs = (CharSequence) in.readValue(CharSequence.class.getClassLoader());
}
我得到以下错误:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling
还有两件事: 1.我已经成功地实现了自己的自定义
Parcelable
对象,问题尤其是
CharSequence
s。
2.我知道
TextUtils.writeToParcel将尽最大努力保存文本格式,我可以接受。

如果其他人感兴趣,我在中找到了答案

使用
TextUtils.writeToParcel(…)
检索存储在
Parcel
中的
CharSequence
的正确方法是

cs = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);

尝试cs=in.readValue(getClass().getClassLoader())@yorkw,您的方法会产生相同的结果(
ClassNotFoundException
)。