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 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.