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 Putting Data in an Intent

Receiving syntax errors with Intent.putExtra command

This challenge is to pass the mFuelLevel value to the intent. I have tried many variations and keep getting this error, or similar:

./LaunchActivity.java:30: error: non-static method putExtra(String,int) cannot be referenced from a static context Intent.putExtra("FUEL_LEVEL",fuel);

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

public class LaunchActivity extends Activity {

  public Button mLaunchButton;
  public int mFuelLevel;

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

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

    mLaunchButton = (Button)findViewById(R.id.launchButton);
    mLaunchButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // Add your code in here!
        startStory(mFuelLevel);

      }
    });
  }
  private void startStory(int fuel){
    Intent.putExtra("FUEL_LEVEL",fuel);
  Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);
        startActivity(intent);
  }
}

2 Answers

Harry James
Harry James
14,780 Points

Hey Robert!

Here, you're trying to run .putExtra() off of the Intent class. Instead, we want to run this off the intent variable. If you look at the code however, you'll notice that the Intent variable doesn't yet exist when this line of code gets run!

To fix the error, we want to run intent.putExtra() after we create the intent variable but before we call startActivity(). We don't want to call it off the class either - that's why the non-static method error gets thrown.


See if you can update your code to do this but if you get stuck along the way, I'll be happy to help out :)

Right on. Amazing what a capital letter can do. Changed the position of the command, worked first try. Thanks!

Harry James
Harry James
14,780 Points

Woohoo! Glad you got it sorted :)

I'd appreciate if you could mark my answer as the Best Answer if it solved your problem - it credits me and also lets other users know that the question has been answered ;)