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

Nikolai Fialkowski
Nikolai Fialkowski
iOS Development with Swift Techdegree Student 10,065 Points

I have reviewed the related forum thread, but still can't nail this quiz... sorry. I'm trying to use the getIntExtra...

Guys, first of all, I appreciate your help.

I have reviewed this forum thread but it didn't help me since I just can't use the getIntExtra method of the intent variable of the Intent data type. It keeps throwing an exception on me. What am I doing wrong? Thank you so much!
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;

    protected void onCreate(Bundle savedInstanceState) {

        // fuelLevel is set elsewhere.
        // Code ommitted for brevity!
        Intent intent = getIntent();
        fuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);

        launchButton = (Button)findViewById(;
        launchButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);

1 Answer

Boban Talevski
Boban Talevski
24,766 Points

You should be adding your code in the onClick method where the app is calling the other activity. We are going from LaunchActivity to FlightActivity and trying to send data to FlightActivity from LaunchActivity. We are not trying to get data which was passed to LaunchActivity (current activity) which is what you are trying to do.

You are getting an intent in the LaunchActivity (this would be an intent used to start this activity) which probably doesn't exist and is null, and then you are trying to invoke getIntExtra on that intent which is null and you are most likely getting a NullPointerException because of that.

So, all you need to do is delete your code where you are trying to get the intent and getting the fuelLevel value from that intent and add this line before starting the new activity (FlightActivity) in the onClick method:

intent.putExtra("FUEL_LEVEL", fuelLevel);

With this, you are "attaching" the value of fuelLevel to the intent and when that intent is used to start the FlightActivity, the value of fuelLevel will be passed along. Inside FlightActivity, you would use the code you have written to actually retrieve the value of fuelLevel that was passed along from LaunchActivity.

Hope this helps.