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 trialgramjoirps
2,048 PointsNow set mFuelLevel the the value from the Intent. Check the Intent documentation if you need help finding the correct me
I couldn't find a way to do that.
Help please
import android.os.Bundle;
public class FlightActivity extends Activity {
public int mFuelLevel = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight);
Intent intent = getIntent();
String mFuelLevel = intent.getStringExtra("FUEL_LEVEL", mFuelLevel);
}
}
3 Answers
haunguyen
14,985 PointsHi,
mFuelLevel is an Int type
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.getIntExtra("FUEL_LEVEL", -1);
}
}
Mikael Enarsson
7,056 PointsTwo things:
- mFuelLevl is already declared as an int, so you can't declare a String with that name.
mFuelLevel = intent.getStringExtra(String [KEY NAME], defaultValue);
- You are using .getStringExtra, but the value you are trying to get is an int. Instead, use .getIntExtra().
mFuelLevel = intent.getIntExtra(String [KEY NAME], defaultValue);
All should be fine after that ^^
gramjoirps
2,048 PointsThanks Mikael,
Your answer was pretty good. Instead of giving me the fish you showed me how to fish.
:) Happy Coding
Mikael Enarsson
7,056 PointsGlad you liked it ^^ I try XD
Mikael Enarsson
7,056 PointsThere seems to be a slight bug in the challenge though, in that you can pass the challenge without assigning the return value of intent.getIntExtra("FUEL_LEVEL", -1) to mFuelLevel.
rocky khan
581 PointsThanks sir, it works for me really thanks alot bcoz i stuck 4 hour in this question.
Roman Kozak
3,119 PointsThe full correct answer is:
import android.os.Bundle;
public class FlightActivity extends Activity {
public int mFuelLevel = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight);
Intent intent = getIntent();
mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);
}
}