2012-03-28 26 views
12

我希望能够将格式化的文本(即格式跨度为文本)从我的一个应用活动发送到另一个应用活动。这些CharSequence实例存在于我创建的某些Parcelable类型内部。如何从包裹中检索使用TextUtils.writeToParcel(...)保存的CharSequence?

例如,编组携带格式化CharSequence的I使用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 ... */ 
} 

的问题是,我不知道如何来检索ParcelCharSequence我私人构造函数。

下不工作:

private HoldsCharSequence(Parcel in) { 
    cs = (CharSequence) in.readValue(CharSequence.class.getClassLoader()); 
} 

我得到以下错误:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling 

两件事:1。 我已经成功地实现自己的定制Parcelable对象,问题在于特别是CharSequence。 2.我知道TextUtils.writeToParcel会尽力保存文本格式,我可以忍受。

+0

尝试CS = in.readValue(的getClass()getClassLoader()); – yorkw 2012-03-28 21:50:51

+0

@yorkw,你的方法会得到相同的结果('ClassNotFoundException')。 – gdecaso 2012-03-29 13:54:38

回答

33

如果有人有兴趣,我在this post找到答案。

来检索已存储在使用TextUtils.writeToParcel(...)一个ParcelCharSequence S上的正确的方法是

cs = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 
相关问题