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 trialKristian Terziev
28,449 PointsIntents 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."
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
19,746 PointsAll 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");
Aimee Ault
29,193 PointsJack'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!
Kristian Terziev
28,449 PointsThanks :)
Gonzalo Nunez
Courses Plus Student 5,064 PointsThanks ! But how do we suppose to know that we have to use that -1? I think the vΓdeo should have mentioned that.
Passavudh Sabpisal
4,474 PointsThank you for helping us with the answer. However, what does the -1 mean?
TERRENCE MAVHUNGA
6,053 PointsThank you it worked but the video did not thought us about that negative 1.
Kristian Terziev
28,449 PointsKristian Terziev
28,449 PointsThank you :)
Ahmed Fahmy
5,295 PointsAhmed Fahmy
5,295 PointsThanks
Filimon Armenis
1,162 PointsFilimon Armenis
1,162 Points;) u rock man ...hehe