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

Android Build an Interactive Story App (Retired) Intents and Multiple Activities Getting Data from an Intent

Michael Foster
Michael Foster
7,801 Points

Need help trying to figure out Build an Interactive Story Challenge task 2 of 2! Please help

I have been stuck at this stage for some time and still can not figure out the solution for this challenge please help me.

FlightActivity.java
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);

     public Intent putExtra ("FUEL_LEVEL", -1)

    // Add your code below!

  }
}

2 Answers

public Intent putExtra ("FUEL_LEVEL", -1)

What is the line of code supposed to be doing? There are a number of syntactic errors here. Can you spot them? (some hints: how do you end a statement in Java? How do you call an object's method? How do you declare a variable of the type Intent?)

The first task is to declare an Intent variable and getting the Intent that was used to start this activity. Recall that you can get the Intent used to start the activity with the method getIntent() which returns the Intent used to start the activity.

We can initialize the variable and assign the Intent used to start the activity in one line:

Intent intentVariable = getIntent();

The next step of the challenge is to assign the mFuelLevel variable the right value from the our Intent object which we previously called 'intentVariable'. Since mFuelLevel is an int, we want to use the getIntExtra() method, using the key from the instructions and the default value, and assign it to mFuelLevel. You can google to find information about various types of getExtra and putExtra methods for Intents.

mFuelLevel = intentVariable.getIntExtra("FUEL_LEVEL", -1);

best -Dave

Lien Truong
Lien Truong
8,292 Points

Task 1 of 2 is

Intent intent = getIntent();

Task 2 of 2 is

int mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);