Bundle containsKey() but returns null on getString()
Back to blogging after a long time, and this time its quite a lame one which I usually wouldn't want to blog about, but I did spend close to an hour trying to figure out what I was doing wrong.
I just started a small android application and was trying to send a paramter from one
So, I had something like this in the first activity:
and something like this in the second activity:
I was a bit disappointed to find out that
which evaluated to true and to support it further, I could even see in the debugger that it was present along with the value which was passed. That's when I started looking at what I did when I inserted the data using
Finally, making the change to ensure that the value which gets passed on is a simple
One note that I would like to make here is that you need to make sure that the type that you put in the bundle is the same type that you try to retrieve, as in, if the value you put in the bundle is an
I just started a small android application and was trying to send a paramter from one
Activity
to another. Now, after a bit of searching around, I found out that I needed to create an Intent
Bundle
and put the parameter in it, and the new Activity
which gets started will be able to fetch it from the same Bundle
using the key that was provided while inserting data into the Bundle
.So, I had something like this in the first activity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
EditText nameText = (EditText) findViewById(R.id.query_textbox);
intent.putExtra("queryKey", nameText.getText());
startActivity(intent);
and something like this in the second activity:
Bundle extras = getIntent().getExtras();
String query = extras.getString("queryKey");
I was a bit disappointed to find out that
query
was null
. I put a check to see if the key "queryKey"
existed in the Bundle
like this:extras.containsKey("queryKey");
which evaluated to true and to support it further, I could even see in the debugger that it was present along with the value which was passed. That's when I started looking at what I did when I inserted the data using
putExtra()
. The value that was being inserted was of the type Editable
but I was trying to retrieve it using getString()
which was clearly not the right thing to do. It was quite silly on my part to have expected that to work earlier.Finally, making the change to ensure that the value which gets passed on is a simple
String
rather than an Editable
object, fixed the issue. This is what I had to do:Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
EditText nameText = (EditText) findViewById(R.id.name_textbox);
intent.putExtra("name", nameText.getText().toString());
startActivity(intent);
One note that I would like to make here is that you need to make sure that the type that you put in the bundle is the same type that you try to retrieve, as in, if the value you put in the bundle is an
int
then use bundle.getInt()
if you don't use the correct one, it will return a null
even though the key exists for it.
Comments
Post a Comment