Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

java - Android getIntent().getExtras() returns null

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...