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 trialAleksandr Zakharov
1,258 PointsIntent Methods
Hello. I used this method: mFuelLevel = intent.getIntExtra("FUEL_LEVEL", 0);
What did I do wrong?
With respect, Aleksandr.
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 = new Intent();
mFuelLevel = intent.getIntExtra("FUEL_LEVEL", 0);
}
}
2 Answers
marchmyth
1,632 PointsYou need to get the intent that was used to start the current activity. i.e.
// instead of this . .
Intent intent = new Intent();
// do this . .
Intent intent = getIntent();
Also, make sure you are passing the correct default value to getIntExtra()
.
Aleksandr Zakharov
1,258 PointsThank you. I just missclicked.
marchmyth
1,632 PointsSo, I did not answer your question?
Aleksandr Zakharov
1,258 PointsNo, everything is OK. Thanks for help.
Brad Jennings
2,158 PointsBrad Jennings
2,158 PointsYou won't be able to get any extras from your intent because you just declared it as a new Intent on the line above. What are you trying to accomplish? Are you trying to create an intent to open a new Activity or possibly broadcast out for broadcast receivers to pick up? Or are you trying to store the fuel level out to disk and then retrieve it when FlightActivity is created?