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 Intents and Multiple Activities Getting Data from an Intent

Cannot understand why my code is not working.

Hi everyone who see my post In Android Basic track, in the Intent and Multiple Activity that step which I need to get data from the start intent, I cannot understand why my code is not working. I did all that I needed but still, it's not working and even I did exactly in the same way the teacher did. So could someone please help me to pass this test? Look forward to any answer :)

FlightActivity.java
import android.os.Bundle;
import android.content.Intent;
public class FlightActivity extends AppCompatActivity {

    public int fuelLevel = 0;

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

        // Add your code below!
      Intent intent = new getIntent();
      fuelLevel = intent.getIntExtra("FUEL_LEVEL");

    }
}

1 Answer

Hi there,

You don't need to call the getIntent() with new; you can leave new out. Then, you need a default value in case getting the extra value fails. The question says to use -1 as the default value. Pass this in as the second parameter like: getIntExtra("FUEL_LEVEL", -1)

I hope that helps,

Steve.

Hi and thanks for the answer So you mean I need to write something like this one?

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

Actually, I tried it but it did not work then I changed the code a little bit and got no result :(

Yep - that's exactly right. I just pasted your code into the challenge where it says to put it and the code passes the test. I suggest restarting the challenge as the compiler might have got itself confused.