2013-01-06 14 views
26

Chciałbym zrobić paczkę klasy A.Jak utworzyć klasę z zagnieżdżonymi obiektami Działka

public class A { 
    public String str; 
    public ArrayList<B> list; 
} 

Oto, do czego do tej pory doszedłem. Jednak zawiesza się z wyjątkiem NullPointerException. Problem stanowią te dwa stwierdzenia: dest.writeList(list); & in.readList(list, this.getClass().getClassLoader());. nie mogę dowiedzieć się, co zrobić z tutaj :(

Klasa A

public class A implements Parcelable { 
    public String str; 
    public ArrayList<B> list; 

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

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(str); 
     dest.writeList(list); 
    } 

    private A(Parcel in) { 
     str = in.readString(); 
     in.readList(list, this.getClass().getClassLoader()); 
    } 

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

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

Klasa B

public class B implements Parcelable { 
    public String str; 

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

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(str); 
    } 

    private B(Parcel in) { 
     str = in.readString(); 
    } 

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

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

Dziękuję za poświęcony czas.

Odpowiedz

43

W końcu zorientowałem się, co wpisać w Google :) i znalazłem to Android, How to use readTypedList method correctly in a Parcelable class?

Zamiast tego rozwiązaniem było użycie read-/writeTypedList. Inicjuję także listę tablic, aby uniknąć dalszego NullPointerException.

Klasa A

public class A implements Parcelable { 
    public String str; 
    public ArrayList<B> list = new ArrayList<B>(); 

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

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(str); 
     dest.writeTypedList(list); 
    } 

    private A(Parcel in) { 
     str = in.readString(); 
     in.readTypedList(list, B.CREATOR); 
    } 

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

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

Klasa B

public class B implements Parcelable { 
    public String str; 

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

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(str); 
    } 

    private B(Parcel in) { 
     str = in.readString(); 
    } 

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

     public B[] newArray(int size) { 
      return new B[size]; 
     } 
    }; 
} 
+0

znak odpowiadasz za przyjęte !! –

+0

Ach tak, to mi nie pozwoliło, a potem o tym zapomniałem :) –

+0

@ Snæbjørn Dzięki za udostępnienie rozwiązania –

18

Jeśli masz tylko jeden Parcelable obiekt wewnątrz głównego Parcelable obiektu, a nie listy jak przyjętym przypadku odpowiedzi. To będzie tak:

klasy A

public class A implements Parcelable { 
    public String str; 
    public B objectB; 

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

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     //The parcelable object has to be the first one 
     dest.writeParcelable(objectB, flags); 
     dest.writeString(str); 
    } 

    private A(Parcel in) { 
     this.objectB = in.readParcelable(B.class.getClassLoader()); 
     str = in.readString(); 
    } 

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

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

Klasa B

public class B implements Parcelable { 
    public String str; 

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

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(str); 
    } 

    private B(Parcel in) { 
     str = in.readString(); 
    } 

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

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

WAŻNE: Należy pamiętać, że aby można pisać i czytać Parcelable Obiekt ma znaczenie. Zamówienie to answer więcej szczegółów

0

Wystarczy nacisnąć ALT + ENTER i zastąpić Parcelable będzie realizować wszystkie niezbędne wdrożenie

0

Możesz dodać Parcelable wtyczki generator kodu z preferencjach, stamtąd można utworzyć parcelable kod płytkę kocioł wykonując: - kliknij prawym przyciskiem myszy nazwę klasy wewnątrz modelu - wybierz generować - wybierz Parcelable

Presto - model będzie aktualizowany wraz z niezbędnym Parcelable standardowy kod.

0

miałem ten sam problem oto generic version

class Item<T : Parcelable> (val model: T, val index: Int) : Parcelable { 

    constructor(parcel: Parcel) : 
     this(parcel.readParcelable(
      Item<T>::model.javaClass.classLoader), 
      parcel.readInt() 
     ) {} 

    override fun writeToParcel(parcel: Parcel?, flag: Int) { 
     parcel?.writeParcelable(model, 0) 
     parcel?.writeInt(index) 
    } 

    override fun describeContents(): Int { 
     return 0 
    } 

    companion object CREATOR : Parcelable.Creator<Item<Parcelable>> { 
     override fun createFromParcel(parcel: Parcel): Item<Parcelable> { 
      return Item(parcel) 
     } 

     override fun newArray(size: Int): Array<Item<Parcelable>?> { 
      return arrayOfNulls(size) 
     } 
    } 
} 
Powiązane problemy