2013-01-06 11 views
26

मैं क्लास ए पार्सेलबल बनाना चाहता हूं।नेस्टेड ऑब्जेक्ट्स के साथ कक्षा कैसे बनाएं पार्सलबल

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

यही वह है जो मैं अब तक आया हूं। हालांकि यह एक NullPointerException के साथ दुर्घटनाग्रस्त हो जाता है। समस्या ये दो कथन हैं: dest.writeList(list); & in.readList(list, this.getClass().getClassLoader());। मैं यहाँ से क्या करना है को समझ नहीं सकता :(

क्लास ए

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

कक्षा बी

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

अपने समय के लिए धन्यवाद।

उत्तर

43

अंततः मुझे पता चला कि Google में क्या टाइप करना है :), और यह पाया गया कि यह Android, How to use readTypedList method correctly in a Parcelable class?

समाधान इसके बजाय read-/writeTypedList का उपयोग करना था। मैं किसी और आगे NullPointerException से बचने के लिए सरणीसूची को भी प्रारंभ करता हूं।

क्लास ए

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

कक्षा बी

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

निशान आप के रूप में स्वीकार का जवाब है था !! –

+0

आह हाँ, यह मुझे नहीं जाने देगा और फिर मैं इसके बारे में भूल गया हूं :) –

+0

@ Snæbjørn समाधान –

18

आप अपने मुख्य Parcelable ऑब्जेक्ट के अंदर केवल एक Parcelable वस्तु है, तो स्वीकार कर लिया उत्तर केस की तरह की सूची नहीं। तो फिर यह निम्नलिखित तरह होगा:

क्लास ए

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

कक्षा बी

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

महत्वपूर्ण: कृपया ध्यान दें आदेश है कि आप लिख सकते हैं और पढ़ा है कि Parcelable ऑब्जेक्ट मायने रखता है। चेकआउट अधिक जानकारी के

के लिए इस answer
0

बस Alt + Enter दबाएं और Parcelable की जगह यह सभी आवश्यक कार्यान्वयन को लागू करेगा

0

आप वरीयता से Parcelable कोड जनरेटर प्लगइन जोड़ सकते हैं, वहाँ से आप parcelable बॉयलर प्लेट कोड बना सकते हैं ऐसा करके: - सही मॉडल के भीतर क्लिक करें वर्ग के नाम - उत्पन्न चयन - चयन Parcelable

Presto - अपने मॉडल आवश्यक Parcelable बॉयलरप्लेट कोड के साथ अद्यतन किया जाएगा।

0

मैं एक ही समस्या यहाँ एक 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) 
     } 
    } 
} 
संबंधित मुद्दे