Ganapathy C
Ganapathy C

Reputation: 5999

Android issue in Parcelable while passing to next activity

i am trying to pass my own Message Class from activity to another activity for that i am using parcelable. while trying to pass through bundle

Activity1 - Passing Bundle

Intent i = new Intent(getApplicationContext(), Activity2.class);
Bundle b=new Bundle();
b.putParcelable("FolderList", mainMP3List)); //***mainMP3List is MusicFolder object***
i.putExtras(b);
startActivityForResult(i, 100); 

Activity2 - Getting Bundle

if(b.containsKey("FolderList"))
mainMP3List = b.getParcelable("FolderList");//Error is the here (readBundle: bad magic number)

i cant able to access them from second activity i am getting this error like

Bundle : readBundle: bad magic number

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

public class MusicFolder implements Parcelable{

private ArrayList<SongsNameList> songsList=new ArrayList<SongsNameList>();  
private ArrayList<MusicFolder> listOfFolders=new ArrayList<MusicFolder>();


public ArrayList<SongsNameList> getSongsList() {
    return songsList;
}

public void addSongInList(SongsNameList song) {
    this.songsList.add(song);
}

public ArrayList<MusicFolder> getListOfFolders() {
    return listOfFolders;
}
public void addFolderInList(MusicFolder folder) {
    this.listOfFolders.add(folder);
}


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    //dest.writeString(category);
    Bundle b = new Bundle();
    b.putParcelableArrayList("songs", songsList);
    b.putParcelableArrayList("folders", listOfFolders);
    dest.writeBundle(b);

}

public static final Parcelable.Creator<MusicFolder> CREATOR = 
        new Parcelable.Creator<MusicFolder>() { 
    public MusicFolder createFromParcel(Parcel in) { 
        MusicFolder category = new MusicFolder();
        //category.listOfFolders = in.readString();
        Bundle b1 = in.readBundle(SongsNameList.class.getClassLoader());        
        category.songsList = b1.getParcelableArrayList("songs");

        Bundle b2 = in.readBundle(MusicFolder.class.getClassLoader());        
        category.listOfFolders = b2.getParcelableArrayList("folders");

        return category;
    }

    @Override
    public MusicFolder[] newArray(int size) {
        return new MusicFolder[size];
    }
};



  }

Upvotes: 3

Views: 1035

Answers (2)

pawelzieba
pawelzieba

Reputation: 16082

Haven't tested it but I think instead of:

    Bundle b1 = in.readBundle(SongsNameList.class.getClassLoader());        
    category.songsList = b1.getParcelableArrayList("songs");

    Bundle b2 = in.readBundle(MusicFolder.class.getClassLoader());        
    category.listOfFolders = b2.getParcelableArrayList("folders");

should be something like:

    Bundle b = in.readBundle(SongsNameList.class.getClassLoader());        
    category.songsList = b.getParcelableArrayList("songs");
    category.listOfFolders = b.getParcelableArrayList("folders");

Upvotes: 3

Graeme
Graeme

Reputation: 25864

This can be caused by you writing values / types into your Parcelable in a certain order and reading it out in different order (One of your data boundaries is being violated).

Check out your save/load order and if you don't see a problem post the code which is applicable to your Parcelable implementation for us to look over.

Upvotes: 0

Related Questions