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

Now set mFuelLevel the the value from the Intent. Check the Intent documentation if you need help finding the correct me

I couldn't find a way to do that.

Help please

FlightActivity.java
import android.os.Bundle;

public class FlightActivity extends Activity {

  public int mFuelLevel = -1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flight);

    Intent intent = getIntent();

    String mFuelLevel = intent.getStringExtra("FUEL_LEVEL", mFuelLevel);

  }
}

3 Answers

Hi,

mFuelLevel is an Int type

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();
    intent.getIntExtra("FUEL_LEVEL", -1);

  }
}
Mikael Enarsson
Mikael Enarsson
7,056 Points

Two things:

  1. mFuelLevl is already declared as an int, so you can't declare a String with that name.
mFuelLevel = intent.getStringExtra(String [KEY NAME], defaultValue);
  1. You are using .getStringExtra, but the value you are trying to get is an int. Instead, use .getIntExtra().
mFuelLevel = intent.getIntExtra(String [KEY NAME], defaultValue);

All should be fine after that ^^

Documentation

Thanks Mikael,

Your answer was pretty good. Instead of giving me the fish you showed me how to fish.

:) Happy Coding

Mikael Enarsson
Mikael Enarsson
7,056 Points

Glad you liked it ^^ I try XD

Mikael Enarsson
Mikael Enarsson
7,056 Points

There seems to be a slight bug in the challenge though, in that you can pass the challenge without assigning the return value of intent.getIntExtra("FUEL_LEVEL", -1) to mFuelLevel.

Thanks sir, it works for me really thanks alot bcoz i stuck 4 hour in this question.

Roman Kozak
Roman Kozak
3,119 Points

The full correct answer is:

import android.os.Bundle;

public class FlightActivity extends Activity {

  public int mFuelLevel = -1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flight);

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


  }
}