5

I'm having a weird problem when I try to send strings with intents when switching activities.

Here is what I do in class 1:

Intent myIntent = new Intent(SearchText.this, Search.class);  
myIntent.putExtra("searchx", spinnerselected);  
myIntent.putExtra("SearchText", input);  
startActivity(myIntent); 

Class 2:

Intent myIntent = getIntent();   
searchText=myIntent.getStringExtra("SearchText");  
spinnerselectednumber=Integer.parseInt(myIntent.getStringExtra("searchx"));

And using the debugger in the second class, its clear that there is a value in searchx.

Though the line myIntent.getStringExtra("searchx") returns null .

Why is this?

C_B
  • 2,620
  • 3
  • 23
  • 45
Omar
  • 7,835
  • 14
  • 62
  • 108

3 Answers3

4

Try to add .ToString() to myIntent.putExtra("searchx", spinnerselected); so that it is myIntent.putExtra("searchx", spinnerselected.ToString()); This always works for me

Aiden Strydom
  • 1,198
  • 2
  • 14
  • 43
3

Was spinnerSelected a String?


From the Javadoc for Intent

public String getStringExtra(String name)

Since: API Level 1

Description: Retrieve extended data from the intent.

Parameters:

name The name of the desired item.

Returns: The value of an item that previously added with putExtra() or null if no String value was found.


There seems to be many ways to retrieve "extras" - whatever the type of spinnerSelected was, try to retrieve it using the appropriate method. Eg if it was an int:

public int getIntExtra(String name, int defaultValue)
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
katsharp
  • 2,551
  • 24
  • 27
0

This code should work:

        Bundle extras = getIntent().getExtras();
        return extras != null ? extras.getString("searchx")
                : "nothing passed in";  
Waza_Be
  • 39,407
  • 49
  • 186
  • 260