0

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.

Community
  • 1
  • 1
Yogesh
  • 1,307
  • 3
  • 13
  • 18

3 Answers3

3

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

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
3

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.

Akram
  • 7,548
  • 8
  • 45
  • 72
1
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..

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64