Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);