Yogesh
Yogesh

Reputation: 1335

Exception while passing extras through intent

I want to pass a string from 1 activity to another, although I have taken reference from many accepted answers from other threads, I am facing problem that I am not able to debug. When I comment extras.putString as shown in code below, Toast message shows the correct address which means value is being set properly and code works fine but when I use extras.putString(), I get NullPointerException and application closes due to exception. There are many \n characters in my address string. Infact even if I use extras.putString("userAddress", "test") I get NullPointerException

Here is my Main Activity from which I want to call FBShare Activity:

Intent mIntent = new Intent(this, FBShare.class);   
Bundle extras = mIntent.getExtras();
String currentAddress = getCurrentAddress(ourLocation);
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show();
extras.putString("userAddress", currentAddress);
startActivity(mIntent);

And in FBShare Activity I am trying to fetch values as follows

strAddress = getIntent().getExtras().getString("userAddress");  

Here is one thread which is doing similar thing.

Upvotes: 0

Views: 464

Answers (3)

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

Intent mIntent = new Intent(this, FBShare.class);   
String currentAddress = getCurrentAddress(ourLocation);
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show();
mIntent.putString("userAddress", currentAddress);
startActivity(mIntent);

Bundle is not need in your case because it seems that you are only pasing a string you can use the above code..

Upvotes: 1

Israel Unterman
Israel Unterman

Reputation: 13510

Try directly putting extra on the intent:

mIntent.putExtra("Key", "Value")

Also, you retrieve the extra using

Intene t = getIntent();
String k="key";
if (t.hasExtra(k)) {
  s = t.getStringExtra(k);
  ...
}

The are get/put for many var types

Upvotes: 3

Akram
Akram

Reputation: 7526

try my code

Intent mIntent = new Intent(this, FBShare.class);   
Bundle extras = new Bundle();
String currentAddress = getCurrentAddress(ourLocation);
Toast.makeText(getBaseContext(), getCurrentAddress(ourLocation), Toast.LENGTH_SHORT).show();
extras.putString("userAddress", currentAddress);
mIntent.putExtras(extras);
startActivity(mIntent);

hope this will work.

Upvotes: 3

Related Questions