Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) Intents and Multiple Activities Getting Data from an Intent

Intents and Multiple Activities -> Getting Data from an Intent challenge. Can't figure out the code.

So here's my code. The challenge says:

"We previously added a FUEL_LEVEL to our spacecraft. Now in FlightActivity, we want to get the value that we put in the Intent. Start by declaring an Intent variable and getting the Intent used to start this Activity."

FlightActivity.java
import android.os.Bundle;

public class FlightActivity extends Activity {

  public int mFuelLevel = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flight);

    // Add your code below!
    Intent intent = getIntent();
    intent.getStringExtra("FUEL_LEVEL");
  }
}

3 Answers

Jack Middlebrook
Jack Middlebrook
19,746 Points

All you need for the first task in the code challenge is to get the intent, which you have correct. Just remove intent.getStringExtra("FUEL_LEVEL");

Thank you :)

Thanks

Jack's correct, but if you're continuing to the next part of that code challenge and do need that line, you'll have a couple of issues. What you really want is:

   mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);

Notice you need to assign it to a variable (mFuelLevel), you need to use getIntExtra instead of getStringExtra (because the value is an integer), and you have to pass in a -1 parameter too, which is the default value. Hope that helps!

Thanks :)

Thanks ! But how do we suppose to know that we have to use that -1? I think the vΓ­deo should have mentioned that.

Thank you for helping us with the answer. However, what does the -1 mean?

Thank you it worked but the video did not thought us about that negative 1.