2014-05-20 20 views
9

otrzymuję następujące błędy:ArrayList nie może być oddane do Parcelable

  • Key com..BookStatus oczekiwanych Parcelable ale wartość była ArrayList
  • ClassCastException: ArrayList nie może być oddane do Parcelable -> i therafter: Nie rozpocząć działalność - NullPointerException
  • ArrayList mają wartości BookStatus przedmiotów (listToPass)

    • Ke y com.example.books.BookStatus oczekiwany Parcelable, ale wartość była java.util.ArrayList. Domyślna wartość została zwrócona
    • Próba oddania wygenerowany wyjątek wewnętrzny:
    • java.lang.ClassCastException: java.util.ArrayList nie mogą być oddane do android.os.Parcelable
    • w com.example.books4locals.SearchActivity. onCreate (SearchActivity.java:46) -> który jest bundle.getparcelable metoda

Co mogę zrobić?

HomeActivity.java

ArrayList<BookStatus> listToPass = new ArrayList<BookStatus>(); // fill with smthg 

     protected void onPostExecute(String result) { 

      if (listToPass != null) { 

       Intent i = new Intent(getApplicationContext(), 
         SearchActivity.class); 
       //    ArrayList<BookStatus> bs = new ArrayList<BookStatus> (Arrays.asList(bArr)); 

       i.putParcelableArrayListExtra("com.example.books.BookStatus", listToPass); 
       startActivity(i); 

SearchActivity.java

  protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.searchactivity_main); 

      Bundle bundle = getIntent().getExtras(); 
      ArrayList<BookStatus> bookStatus = bundle 
        .getParcelable("com.example.books.BookStatus"); 

BookStatus.java

  public class BookStatus implements Parcelable { 

     public BookStatus() { 
     } 

     public BookStatus(Parcel in) { 
      this.bookId=in.readInt(); 

      this.address=in.readString(); 
      this.name=in.readString(); 
      this.author=in.readString(); 
      this.lenderUserId=in.readInt(); 
      this.isbn=in.readInt(); 
      this.postcode=in.readString(); 
      this.town=in.readString(); 
      this.telnumber=in.readString(); 
      this.mail=in.readString(); 
      this.duration=in.readInt(); 
      } 
         @Override 
      public int describeContents() { 
       // TODO Auto-generated method stub 
       return 0; 
      } 

      @Override 
      public void writeToParcel(Parcel dest, int flags) { 
       dest.writeInt(this.bookId); 

       dest.writeString(this.address); 
       dest.writeString(this.name); 
       dest.writeString(this.author); 
       dest.writeInt(this.lenderUserId); 
       dest.writeInt(this.isbn); 
       dest.writeString(this.postcode); 
       dest.writeString(this.town); 
       dest.writeString(this.telnumber); 
       dest.writeString(this.mail); 
       dest.writeInt(this.duration); 

      } 

      public void readFromParcel(Parcel parcel){ 
        this.bookId = parcel.readInt(); 
        this.address = parcel.readString(); 

        this.name = parcel.readString(); 
        this.author = parcel.readString(); 
        this.lenderUserId = parcel.readInt(); 
        this.isbn = parcel.readInt(); 
        this.postcode = parcel.readString(); 
        this.town = parcel.readString(); 
        this.telnumber = parcel.readString(); 
        this.mail = parcel.readString(); 
        this.duration = parcel.readInt(); 
        } 




      public static final Parcelable.Creator<BookStatus> CREATOR = new Parcelable.Creator<BookStatus>() { 

       public BookStatus createFromParcel(Parcel in) { 
      return new BookStatus(in); 
     } 

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

Odpowiedz

21

próba zmiany

i.putParcelableArrayListExtra("com.example.books.BookStatus", listToPass); 

do

i.putExtra("com.example.books.BookStatus", listToPass); 

i

bundle.getParcelable 

do

bundle.getParcelableArrayList 
+0

to działało dobrze, thx :) – flyOWX

+0

mile widziane, chętnie pomoże –

Powiązane problemy