Search code examples
javaandroidandroid-activityparcelabletransfer

BadParcelableException


I am trying to parcel some data from the main activity to another activity and present the data in a ListView. I have a single button in my MainActivity which creates and intent and ships the data to the other activity. However, as soon as I press the button the app crashes with BadParcelableException. Could somebody explain to me why this is happening?

This is my code in MainActivity:

public class MainActivity extends Activity {
private Button mButton;
public static String EXTRA_KEY = "key_extra";
private Person[] mPersons;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPersons = new Person[1];

    mPersons[0] = new Person("GEORGI",23);
    mPersons[0] = new Person("Mariya",21);

    mButton = (Button) findViewById(R.id.parcelBTN);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,ReceiveParcelActivity.class);
            intent.putExtra(EXTRA_KEY,mPersons);
            startActivity(intent);
        }
    });
}

And this is the second Activity:

public class ReceiveParcelActivity extends ListActivity {
private Person[] mPersons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receive_parcel);

    Intent intent = getIntent();
    Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.EXTRA_KEY);

    mPersons = Arrays.copyOf(parcelables,parcelables.length,Person[].class);

    ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this,android.R.layout.simple_list_item_1,mPersons);

    setListAdapter(adapter);
}

And this is the Person class which implements Parcelable interface

public class Person implements Parcelable {
private String mName;
private int mAge;

public Person(String name, int age) {
    mName = name;
    mAge = age;
}

public Person(Parcel parcel) {
    mName = parcel.readString();
    mAge = parcel.readInt();
}

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

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

private static Creator<Person> CREATOR = new Creator<Person>() {
    @Override
    public Person createFromParcel(Parcel source) {
        return new Person(source);
    }

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

Solution

  • I figured what was wrong. I declared the Creator private. After declaring it public it fixed the problem. However, now I don't know how to present the data into the ListActivity, I mean, now there is only one row and that is the memory address of the Person object. enter image description here