Android Intent.getStringExtra() returns null
Asked Answered
G

1

49

This is how strings are being added to Extras:

Intent i = new Intent();
i.putExtra("Name", edt_name.getText());
i.putExtra("Description", edt_desc.getText());
i.putExtra("Priority", skb_prior.getProgress());
setResult(RESULT_OK, i);
finish();

This is how I try to extract them in onActivityResult():

String name = data.getStringExtra("Name");
String desc = data.getStringExtra("Description");
int prior   = data.getIntExtra("Priority", 50);

But after the second code block name and desc are null's, though prior has it's proper value. Moreover, in debugger I can see, that data.mExtras.mMap contains needed Strings, but only after first request to it.

Gynoecium answered 21/3, 2013 at 18:49 Comment(2)
Are you sure prior has proper value and not default one (50) which you are setting?Iolanthe
Yes, but the problem was in edt_name.getText(), which returns not String but some heir class, may be?Gynoecium
P
104

When you insert your Extras trying adding .toString()

i.putExtra("Name", edt_name.getText().toString());

You are seeing the CharSequence value in there but you need to convert it to a String to call getStringExtra(). Obviously, just do this for the Strings. You see the correct value for your int because that is done correctly

Pliocene answered 21/3, 2013 at 18:54 Comment(5)
Thanks, it works, as well as "" + edt_name.getText(), which looks not as pretty :)Gynoecium
It works because "" is a String literal and you're adding to it. It works the same way as "" + 2 comes out to "2". Whatever is being added to the literal is converted to a String via their own class's toString function.Checkerwork
@codeMagic, Why do you say that it's an object? Isn't it CharSequence?Reflector
@Reflector Yes, it would actually return a charSequencePliocene
@Pliocene In my case, issue is not this one though, as I tried to use same receiver for different Alarms this issue triggered, don't know why? but I end up adding each individually it worked, the purpose of intent in this new approach is now gone..Alurta

© 2022 - 2024 — McMap. All rights reserved.