Android Build an Interactive Story App Intents and Multiple Activities Putting Data in an Intent

Anthony Beattie
Anthony Beattie
3,720 Points

code is not working to pass fuel level to flight activity. can anyone help please?

I've tried to look through my code, checking for capitals etc. its not passing, is there something I'm not seeing?

LaunchActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class LaunchActivity extends AppCompatActivity {

    public Button launchButton;
    public int fuelLevel;

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

        // fuelLevel is set elsewhere.
        // Code ommitted for brevity!

        launchButton = (Button)findViewById(R.id.launchButton);
        launchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Add your code in here!
                Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);
                intent.putExtra("FUEL_LEVEL", mFuelLevel);
                startActivity(intent);
            }
        });
    }
}

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Anthony!

When your code fails, you can usually click on the 'Preview' button to see what the error was. In your case, your variable is declared as 'fuelLevel', but you're trying to use 'mFuelLevel' :)

Anthony Beattie
Anthony Beattie
3,720 Points

Thank you! On a similar note, is it better to know the basics of java before taking this course? although its called beginners android, it feels like I need to know what all the code means to follow along. However, if this is as simple as it gets, ill push through.

Ben Deitch
Ben Deitch
Treehouse Teacher

Yep, having a good understanding of Java really helps when it comes to understanding Android! There's 3 java courses listed as prereqs to the Build a Simple Android App course; they should be listed near the top.