3

I want to pass a parcelable object to an activity to another one. So I implemented a parcelable class to accomplish this. And I coded:

Intent intent = new Intent(mainactivity.this, SecondActivity.class);
Object[] object = new Object();
intent.putExtra("Object ", object);
startActivity(intent);

and in the second activity I coded:

Object[] object = (Object[]) getIntent().getExtras().getParcelable("object");

When I pass the object to Intent, it's not null. While, when I get it in the second Activity is null. Do you have any suggestions? Thanks in advance!

4

2 回答 2

8

Change this:

Object object = (Object) getIntent().getExtras().getParcelable("object");

To this:

Object object = getIntent().getExtras().get("Object");

getParcelable should only be used if you are using putParcelable or inserting a Parcelable object using putExtra in the sending part of code.

P.S. also mind the difference of key-name Object and object

于 2012-08-31T08:20:32.810 回答
2

您可以简单地在调用者中使用:

Intent i = new Intent(EditActivity.this, ViewActivity.class);
i.putExtra("myObj", p);
startActivity(i);

在接收器中:

Bundle b = i.getExtras();
Person p = (Person) b.getParcelable("myObject");

希望这对你有帮助

于 2012-08-31T10:48:00.790 回答