2011-07-21 46 views
28

我有兩個活動,在第一個,我instanciate對象myObject的ArrayList。在第二項活動中,我需要獲得這位Arraylist。我找到了一個教程:http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html 我已經實現了我的課,喜歡它的解釋。Android,如何在Parcelable類中正確使用readTypedList方法?

公共類Chapitre實現Parcelable {

private int numero; 
private String titre; 
private String description; 
private int nbVideo; 
private ArrayList<Video> listeVideo; 

public Chapitre(int numero, String titre, String description, 
     ArrayList<Video> listeVideo) { 
    this.numero = numero; 
    this.titre = titre; 
    this.description = description; 
    this.listeVideo = listeVideo; 
    this.nbVideo = listeVideo.size(); 
} 

//Getters and Setters ... 

private Chapitre(Parcel source) { 
    numero = source.readInt(); 
    titre = source.readString(); 
    description = source.readString(); 
    nbVideo = source.readInt(); 
    source.readTypedList(listeVideo, Video.CREATOR); 
} 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeInt(numero); 
    dest.writeString(titre); 
    dest.writeString(description); 
    dest.writeInt(nbVideo); 
    dest.writeTypedList(listeVideo); 
} 

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
    public Chapitre createFromParcel(Parcel in) { 
     return new Chapitre(in); 
    } 

    public Chapitre[] newArray(int size) { 
     return new Chapitre[size]; 
    } 
}; 

}

公共類視頻實現Parcelable {

private String titre; 
private int numero; 
private String url; 
private String description; 
private String imageUrl; 
private Bitmap image; 
private String duree; 

/** 
* 
* @param nom 
* @param numero 
* @param url 
* @param description 
*/ 
public Video(String titre, String url, String description) { 
    super(); 
    this.titre = titre; 
    this.url = url; 
    this.description = description; 
} 

public Video(int numero, String titre, String url, String description) { 
    super(); 
    this.titre = titre; 
    this.url = url; 
    this.description = description; 
    this.numero = numero; 
} 

public Video(String titre,int numero, String url, String description, String imageUrl) { 
    super(); 
    this.titre = titre; 
    this.url = url; 
    this.description = description; 
    this.imageUrl = imageUrl; 
    this.numero = numero; 

    setImage(fetchImage(imageUrl)); 
} 


//Getters and Setters ... 

@Override 
public int describeContents() { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(titre); 
    dest.writeInt(numero); 
    dest.writeString(url); 
    dest.writeString(description); 
    dest.writeString(imageUrl); 
    dest.writeString(duree); 


} 

public Video(Parcel source){ 
    /* 
    * Reconstruct from the Parcel 
    */ 
    Log.v("TAG", "ParcelData(Parcel source): time to put back parcel data"); 
    titre = source.readString(); 
    numero = source.readInt(); 
    url = source.readString(); 
    description = source.readString(); 
    imageUrl = source.readString(); 
    duree = source.readString(); 
} 

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
    public Video createFromParcel(Parcel in) { 
     return new Video(in); 
    } 

    public Video[] newArray(int size) { 
     return new Video[size]; 
    } 
}; 

}

,但我得到nullPointException就行了「源。 readTypedList(listeVideo,Video.CREATOR);「在Chapitre班。

07-21 10:07:28.212: ERROR/AndroidRuntime(682): FATAL EXCEPTION: main 
07-21 10:07:28.212: ERROR/AndroidRuntime(682): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.genicorp.video.proto/com.genicorp.video.proto.Lecture}:  java.lang.NullPointerException 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Looper.loop(Looper.java:126) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.app.ActivityThread.main(ActivityThread.java:3997) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at java.lang.reflect.Method.invoke(Method.java:491) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at dalvik.system.NativeStart.main(Native Method) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682): Caused by: java.lang.NullPointerException 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Parcel.readTypedList(Parcel.java:1630) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at com.genicorp.video.proto.Chapitre.<init>(Chapitre.java:70) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at com.genicorp.video.proto.Chapitre.<init>(Chapitre.java:65) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at com.genicorp.video.proto.Chapitre$1.createFromParcel(Chapitre.java:89) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at com.genicorp.video.proto.Chapitre$1.createFromParcel(Chapitre.java:1) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Parcel.readParcelable(Parcel.java:1981) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Parcel.readValue(Parcel.java:1846) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Parcel.readListInternal(Parcel.java:2092) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Parcel.readArrayList(Parcel.java:1536) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Parcel.readValue(Parcel.java:1867) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Parcel.readMapInternal(Parcel.java:2083) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Bundle.unparcel(Bundle.java:215) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.os.Bundle.getParcelableArrayList(Bundle.java:1151) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.content.Intent.getParcelableArrayListExtra(Intent.java:3634) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at com.genicorp.video.proto.Lecture.onCreate(Lecture.java:37) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700) 
07-21 10:07:28.212: ERROR/AndroidRuntime(682):  ... 11 more 

我已經廢棄物1一天,在這一點,因此,如果有人可以幫助我,這將是巨大的,

在此先感謝

+4

不確定這是否重要,但有些公司會因看到太多信息而皺眉。從這個未經清理的輸出中,人們可以根據您的其他問題推斷出您的工作對象,以及您正在製作的應用程序的類型。消毒你的輸出也會幫助你找到你的錯誤。 – Kheldar

回答

71

希望你現在想通了這一點。但對於任何一個在這個問題上磕磕絆絆的人。你得到的NullPointerException是由於ArrayList永遠不會被初始化。

這將解決這個問題:

private Chapitre(){ 
    listVideo = new ArrayList<Video>(); 
} 

private Chapitre(Parcel source) { 
    // Call the above constructor 
    this(); 

    numero = source.readInt(); 
    titre = source.readString(); 
    description = source.readString(); 
    nbVideo = source.readInt(); 
    source.readTypedList(listeVideo, Video.CREATOR); 
} 
+6

完美回覆。主席先生,你應該得到一個'驗證'的答案! –

+0

這是上述問題的完美解決方案。 – Seenu69

5

另一種解決方案:使用createTypedArrayList代替readTypedList這需要一個非空列表對象引用

-1

也可以考慮用一個不可變空列表初始化的對象。

private ArrayList<Video> listevideo = Collections.emptyList()

更多細節在這裏:Collections.emptyList() vs. new instance

+0

由於'readTypedList()'填充了您提供的列表,因此從'Collections.emptyList()'提供一個不可變的列表將導致引發'UnsupportedOperationException'。 – spaaarky21

+0

對不起,我應該澄清 - 初始化以表明任何人想要使用該列表的合約必須創建一個新合約 –

0

也請記住,你必須閱讀並以相同的順序寫parcelable對象屬性! 我也2個小時,因爲解組不是相同的編組順序。