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
Ryan Hemrick
12,759 PointsGetting Data from an Intent. Challenge 2 of 2
Now set mFuelLevel the the value from the Intent. Check the Intent documentation if you need help finding the correct method to use. Use "FUEL_LEVEL" as the key and -1 as the default value.
Not sure why my code is not passing:
// Add your code below!
Intent intent = getIntent();
mFuelLevel = intent.getIntExtra("FUEL_LEVEL");
if (mFuelLevel == null) {
mFuelLevel = -1;
}
2 Answers
Jack Middlebrook
19,746 PointsThe documentation shows getIntExtra as taking two parameters:
public int getIntExtra (String name, int defaultValue)
It looks like in your code you are just passing it one parameter so be sure to add the defaultValue. Once you do this, you won't need the if statement.
Zlatko Iliev
12,236 PointsI think first you have to declare the mFuelLevel variable as an integer and set the default value -1 as a default parameter:
Intent intent = getIntent();
int mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);
This should do the work!
Ryan Hemrick
12,759 PointsHey! I believe the mFuelLevel variable is declared before the code snippet that I included. Jack's solution worked for me.
Thanks for the response
Ryan Hemrick
12,759 PointsRyan Hemrick
12,759 PointsI can't believe I overlooked that! Thanks!