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
W Joel Baker
1,012 PointsFlightActivity FUEL_LEVEL.
I'm trying to use the previous lesson's code as it states in the challenge. However my code isn't working. What am I missing?
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 = intent.getIntExtra("FUEL_LEVEL");
startActivity (intent);
}
}
The error I'm getting is...
./FlightActivity.java:14: error: method getIntExtra in class Intent cannot be applied to given types;
mFuelLevel = intent.getIntExtra("FUEL_LEVEL");
^
required: String,int
found: String
reason: actual and formal argument lists differ in length
1 error
4 Answers
Steve Hunter
57,712 PointsHi Joel,
The intent takes a key/value pairing to pass this information along. You also use a putExtra method for it.
The code I'd use to send the required value along the intent would be:
intent.putExtra("FUEL_LEVEL", mFuelLevel);
The value of mFuelLevel is being passed into the new Activity by the intent. Its value already exists in this Activity so there's no need to try to assign a value to it from the intent at this point. The intent is being used to 'transmit' the value of mFuelLevel into the next Activity, it can be used at the other end by using the key, "FUEL_LEVEL" to access the value using a key/value pairing.
Hope this helps.
Steve.
Steve Hunter
57,712 PointsOK - so we've had the change in Activity so we're now in the new one where none o the previous code means a thing - that is all out of scope.
However, we did transmit some information with the intent and we now want to access that.
In order to use the intent functionality, we first need to create an intent in this scope. The first line that's required, therefore, is
Intent intent = getIntent();
That does what it says on the tin - it creates a variable called intent and assigns it the intent used to create the Activity that we're in.
We now want to populate the intent with a value; the one we sent over when the Activity was changed. You have correctly done this bit but not assigned it in the task. We want to assign the transmitted value to the variable mFuelLevel. We can do that by using a method that comes with the intent class. You've correctly selected getIntExtra().
That takes two parameters. Firstly, the key for the key/value pair that was sent over. You've identified that as being "FUEL_LEVEL". Then there's a default value in case things go wrong. The question says that we should use -1. So either the value or -1 needs to be assigned to the local variable mFuelLevel. That looks like:
mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);
Those two lines complete the challenge.
Steve.
W Joel Baker
1,012 PointsAh! Replace mFuelLevel = intent.getIntExtra("FUEL_LEVEL"); because the intent already exists. We need to make it work by using with intent.putExtra("FUEL_LEVEL", mFuelLevel);
I'll need to read your reason why a few times before I get a better understanding of it. I am wondering why the lessons don't seem to match up with what the challenges though.
Steve Hunter
57,712 PointsYes, the intent already exists, as does the mFuelLevel variable value.
The challenge is to get the value of mFuelLevel passed to the new Activity - it is currently out of scope to it. So the intent can be used to both bring the new Activity into view and also to carry values through to it.
So, we can add 'extras' to the intent allowing values to get piggybacked into the new Activity.
W Joel Baker
1,012 PointsI'm trying to get the second part of the challenge now. I read the documentation http://developer.android.com/reference/android/content/Intent.html#getIntExtra%28java.lang.String,%20int%29 but I'm missing something somewhere.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight);
// Add your code below!
intent.getIntExtra ("FUEL_LEVEL", -1);
Intent intent = getIntent();
intent.putExtra("FUEL_LEVEL", mFuelLevel);
startActivity (intent);
}
}
To be honest I thought I had it with "intent.getIntExtra ("FUEL_LEVEL", -1);" but obviously not.
I got it! Just in the wrong spot. Needed to be below "intent.putExtra("FUEL_LEVEL", mFuelLevel);"
Thank you for your help Steve! When I read your replies it might take me a bit but I start to get it figured out. Just takes me time.
adamghani
7,955 Pointsadamghani
7,955 PointsHello W, please post the question and the error you're getting.