开发者

myIntent.getStringExtra returns null? [duplicate]

This question already has an answer here: Android Intent.getStringExtra() returns null (1 answer) Closed 1 year ago.

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 i开发者_如何学运维s a value in searchx.

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

Why is this?


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


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)


This code should work:

        Bundle extras = getIntent().getExtras();
        return extras != null ? extras.getString("searchx")
                : "nothing passed in";  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜