2013-01-06 33 views
26

Tôi muốn làm cho class A Parcelable.Làm thế nào để tạo một lớp học với các đối tượng lồng nhau Parcelable

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

Đây là những gì tôi đã đưa ra cho đến nay. Tuy nhiên nó bị treo với một NullPointerException. Vấn đề là hai câu sau: dest.writeList(list); & in.readList(list, this.getClass().getClassLoader());. tôi không thể tìm ra những việc cần làm từ đây :(

Class 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]; 
     } 
    }; 
} 

Class 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]; 
     } 
    }; 
} 

Cảm ơn bạn đã dành thời gian.

Trả lời

43

Cuối cùng tôi đã tìm ra những gì để nhập vào Google :) và tìm thấy điều này Android, How to use readTypedList method correctly in a Parcelable class?

Giải pháp là sử dụng read-/writeTypedList để thay thế. Tôi cũng khởi tạo arraylist để tránh bất kỳ NullPointerException nào khác.

Class 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]; 
     } 
    }; 
} 

Class 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

bạn trả lời như chấp nhận !! –

+0

Ah vâng, nó sẽ không cho phép tôi và sau đó tôi quên mất nó :) –

+0

@ Snæbjørn Cảm ơn bạn đã chia sẻ giải pháp –

18

Nếu bạn có chỉ có một đối tượng Parcelable bên trong đối tượng chính Parcelable của bạn, không liệt kê như trường hợp câu trả lời chấp nhận. Sau đó, nó sẽ giống như sau:

Class 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]; 
     } 
    }; 
} 

Class 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]; 
     } 
    }; 
} 

QUAN TRỌNG: Xin lưu ý rằng theo thứ tự mà bạn viết và đọc Parcelable đối tượng quan trọng. Thanh toán answer này để biết thêm chi tiết

0

Chỉ cần nhấn ALT + ENTER và thay thế Parcelable nó sẽ thực hiện tất cả việc thực hiện cần thiết

0

Bạn có thể thêm mã cắm máy phát điện Parcelable từ prefs, từ đó bạn có thể tạo mã tấm nồi hơi parcelable bằng cách thực hiện: - nhấp chuột phải vào tên lớp trong mô hình - chọn tạo - chọn Bưu kiện

presto - mô hình của bạn sẽ được cập nhật bằng mã bản mẫu cần thiết.

0

tôi đã cùng một vấn đề ở đây là một dấu 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) 
     } 
    } 
} 
Các vấn đề liên quan