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
)。