2012-03-28 51 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); 
相關問題