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 trialShiv Anant Yadav
1,064 PointsHow to receive int as in intent?
I'm confused in bringing back an int as an intent.
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();
mFuelLevel = putExtra(FUEL_LEVEL, -1);
}
}
1 Answer
Steve Hunter
57,712 PointsHi Shiv,
You're very close with that.
You've declared a new Intent
and are trying to extract the extra
from it. You've not selected the correct method, though. We're getting (an integer), not putting. And the FUEL_LEVEL
isn't a variable name, it is a key in a key/value pair, so it is a string; you'll need to surround it with double-quotes.
So, your code should end up with something like:
Intent intent = this.getIntent();
mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);
I hope that helps.
Steve.
Shiv Anant Yadav
1,064 PointsShiv Anant Yadav
1,064 PointsThank you so much Steve. I have been stuck at the same place for about a day I guess. On point explanation which cleared all my doubts. Thanks again :)
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHey, no problem! Don't struggle for a whole day! Just shout in here, we'll come and help. If you get no joy, remember you can @ mention me directly.
Shiv Anant Yadav
1,064 PointsShiv Anant Yadav
1,064 PointsYes, I was actually trying and trying and trying to do it on my own, tried all the methods I could see on the official website of android which said int. Then I saw that I can look for help here too, so I immediately asked. Thanks for the support.