2014-02-07 59 views
0

我正在試圖爲以下字段的類實現parcelable。當我嘗試從字段String(String mStyle []和byte [] mImage)中創建包(放入包中)的類時,我得到一個異常。我在writeToParcel的這些字段上沒有收到任何錯誤 - 什麼是正確的用法?。感謝很多如何使用String []和byte []字段爲類實現Parcelable?

 private String mClothingItemName; 
     private String mColor; 
    private String mPrint; 
    private String mStyle[]; 
    private String mMaterial; 
    private String mBrand; 
    private String mStorageLocation; 
    private String mOptionalDescription; 

    private byte[] mImage; 




    public void writeToParcel(Parcel out, int flags) { 
     out.writeString(mClothingItemName); 
     out.writeLong(mClothesId); 
     out.writeString(mColor); 
     out.writeString(mPrint); 
     //out.writeStringArray(mStyle); 
     out.writeString(mMaterial); 
     out.writeString(mBrand); 
     out.writeString(mStorageLocation); 
     out.writeString(mOptionalDescription); 
    // out.writeByteArray(mImage); 
    } 


    private Clothes(Parcel in) { 

     mClothingItemName = in.readString(); 
     mClothesId = in.readLong(); 
     mColor = in.readString(); 
     mPrint = in.readString(); 
//  in.readStringArray(mStyle); 
     mMaterial = in.readString(); 
     mBrand = in.readString(); 
     mStorageLocation = in.readString(); 
     mOptionalDescription = in.readString(); 

     //in.readByteArray(mImage); 

    } 
+0

你會得到哪些例外? –

+0

@ Goopal rao --- 02-07 06:41:51.653:E/AndroidRuntime(3150):引起:java.lang.NullPointerException 02-07 06:41:51.653:E/AndroidRuntime(3150):\t at android.os.Parcel.readStringArray(Parcel.java:931) 02-07 06:41:51.653:E/AndroidRuntime(3150):\t at abacus.dmbasys.closetorganizer.Clothes。 (Clothes.java:323) – user3013499

回答

1

我推薦您使用本網站link,從一個類將生成代碼,使其parceable。

+0

@ TeRRo - 謝謝,我現在試過了,但是它將所有其他字段放在一起,並給出了這兩個字段的以下錯誤-----以下變量未被識別(語法錯誤? ): 私人字符串[] mStyle 私人字節[] mImage – user3013499

+0

Your've複製了類的代碼,你堅持在網站上?要複製這兩個變量,構造函數,getter和setter。 – TeRRo

+0

我剛剛複製了變量並卡在了網站上(即時在這個類中實現了更多的接口,我將離開它了嗎?),好的,我會嘗試使用變量,構造函數,getter和setter – user3013499

相關問題