开发者

getParcelable() returns null object

I've used putExtra() to pa开发者_高级运维ss some data from one activity to other. I want to create a Parcelable instance but I am getting a null object.

Here is the code of first activity:

i = new Intent(Activity1.this, Activity2.class);
i.putExtra(com.login_app.Activity1.extra, "100");
startActivity(i);   

Here is the code of second activity:

Intent inew = getIntent();
Bundle icicle1 = inew.getExtras();
// this is just a debug code
System.out.println(
    icicle1.getSerializable(com.login_app.Activity1.extra).toString());
Parcelable p = inew.getParcelableExtra(com.login_app.Activity1.extra);

Here object p is a null object.

Please tell me if I am wrong or I need to add something else. I want this Parcelable object to be flattened into a Parcel object.


That's because you've put String, but trying to get Parcelable. You should use getStringExtra instead.

Also, from Bundle documentation of [getParcelable()][1] (this function is used to actually get extra from Intent's bundle):

Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

So you basically get null because you have type mismatch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜