40

I'm trying to pass a string between two activities. I've done this in other projects using the same method, but for some reason I'm getting a NullPointerException when I call intent.getStringExtra(String). I have also tried creating a Bundle for the extras via

Bundle b = getIntent().getExtras();

but that also returned null. Below is the code that I am currently trying to use.

Activity A:

Intent myIntent = null; 
    String select = "";
            if (selection.equals("Chandelle")) {
                myIntent = new Intent(Commercial.this, Chandelle.class);
                select = "Chandelle";
            } else if (selection.equals("Eights on Pylons")) {
                myIntent = new Intent(Commercial.this, EightsOnPylons.class);
                select = "Eights on Pylons";
            }
 // Start the activity
    if (myIntent != null) {
        myIntent.putExtra("selection", select);
        Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection"));
        startActivity(myIntent);
    }

Here's the code in activity B that tries to pull the extra:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    Intent i = getIntent();

    if (i == null) 
        Log.d("***DEBUG****", "Intent was null");
    else
        Log.d("**** DEBUG ***", "Intent OK");

    String MANEUVER_ID  = i.getStringExtra("selection"); //Exception points to this line
    Log.d("*** DEBUG", rec + " " + MANEUVER_ID);

I've tried pretty much every alternative way of passing extras, but they all seem to behave this way. What am I missing?

FlapsFail
  • 525
  • 1
  • 5
  • 11

7 Answers7

14

Add .ToString() to myIntent.putExtra("selection", select); so that it is myIntent.putExtra("selection", select.ToString());

Aiden Strydom
  • 1,198
  • 2
  • 14
  • 43
  • This also fixed my problem, as I was passing in EditText.getText() which is an Editable object and even though putExtra() takes two strings, the compiler doesn't complain about passing it an Editable object. – glenneroo Feb 20 '14 at 21:07
  • 1
    The reason you need to put String.valueOf() for the Editable object returned by EditText.getText() is that it maps direclt to a Serializable, so putExtra(String, Serializable) is used and therefore getSerializableExtra(String) would need to be called. – TheIT Aug 05 '14 at 20:45
  • 1
    \|/(>_<)\|/... i need a superhero name... :) – Aiden Strydom Nov 19 '14 at 21:22
  • 7
    Could you please edit to explain why this is necessary? Since `select` is already declared as a string, why does he need to invoke `toString`? – MirroredFate Sep 22 '15 at 20:05
  • so what is the reason that we should put `toString()` since `select` is already declared as a string – zihadrizkyef Jan 24 '17 at 00:15
  • the toString method returns this. I don't get why that is necessary. – jeff Jun 28 '17 at 09:46
8

Luckily I found this post. I've been struggling with this for hours and in the end I realized that I'm sending my intent to the tabHost as well. ;-) For those who are still having problems with this just get the extras from the tabHost activity itself and pass it along to the child tabs.

String userID;

. .

 Bundle getuserID = getIntent().getExtras();
    if (getuserID != null) {
        userID = getuserID.getString("userID");
    }

...

 intent = new Intent().setClass(this, Games.class);
    intent.putExtra("userID", userID);
    spec = tabHost.newTabSpec("games").setIndicator("Games",
                      res.getDrawable(R.drawable.tab_games))
                  .setContent(intent);
    tabHost.addTab(spec);
Stark
  • 2,581
  • 2
  • 17
  • 20
4

Try the code below. I have added an override to your code, which will do the trick:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 

   Intent i = getIntent();

   if (i == null) 
       Log.d("***DEBUG****", "Intent was null");
   else
       Log.d("**** DEBUG ***", "Intent OK");

   String MANEUVER_ID  = i.getStringExtra("selection"); //Exception points to this line
   Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
}
Skywalker
  • 1,717
  • 1
  • 22
  • 25
2

If you still have not put anything in the bundle, it will always return null!

You can create and set the bundle by your own :

Intent i = new Intent( ... );

i.putExtras(new Bundle());

Bundle bundle= i.getExtras(); // (not null anymore)

Or, put a dummy value just to force the initialization (I prefer the first solution):

Intent i = new Intent( ... );

i.putExtra("dummy", true); 

Bundle bundle= i.getExtras(); // (not null anymore)
jAC
  • 5,195
  • 6
  • 40
  • 55
Marcelo C.
  • 3,822
  • 2
  • 22
  • 11
  • I think the first case *can* be null after process death. The second option won't be null. That dummy variable is what I called `__EXISTENCE_HOOK__` a while ago for this reason. – EpicPandaForce Oct 11 '18 at 19:35
2

My problem was resolved when I figured out not to startAcitvity() before I used putExtra(). Newbie mistake but I hope somebody will find it useful.

I changed this

Intent i1 = new Intent(MainActivity.this,Second.class);
    startActivity(i1);
    String abc = e1.getText().toString();
    i1.putExtra("user",abc);

to this

Intent i1 = new Intent(MainActivity.this,Second.class);
    String abc = e1.getText().toString();
    i1.putExtra("user",abc);
    startActivity(i1);
Peter
  • 51
  • 4
2

Look at your code, I think it may happen when you did put anything in Activity 1. Because you use "if...else if", and you don't have "else" for other cases. If that happens then you will get null value in the Activity 2. Check again this case.

Xuvi
  • 519
  • 3
  • 9
  • 1
    No luck. I should've been more specific in that activity B that I posted is actually EightsOnPylons.java. Also, the Log.d statement in activity one that prints "OUTBOUND INTENT" does show the correct String, so I'm pretty sure the extra is actually being sent with the intent. – FlapsFail May 10 '11 at 02:49
  • 8
    Found the problem. It was an issue with the design of the program, not with the code. Activity B was a tabhost that called Activity C. I was trying to pull the extra from Activity C but the extra had only been sent from A to C. – FlapsFail May 10 '11 at 03:03
-1

In activity B you at the following function:

  protected String getStringExtra(Bundle savedInstanceState, String id) {
    String l;
    l = (savedInstanceState == null) ? null : (String) savedInstanceState
            .getSerializable(id);
    if (l == null) {
        Bundle extras = getIntent().getExtras();
        l = extras != null ? extras.getString(id) : null;
    }
    return l;
}

You are using it in the following way in activity B:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState); 
   String MANEUVER_ID  = getStringExtra(savedInstanceState, "selection");      
}
Skywalker
  • 1,717
  • 1
  • 22
  • 25