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!

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

Activity is asking for a "key" to be used - FUEL_LEVEL and the compiler says an error, but in preview there is no info.

The question is worded in a way that makes it hard to understand and the preview section shows no output on compiler error.

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;

  protected void onCreate(Bundle savedInstanceState) {

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

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

I figured it out. I didnt understand that the word "key" referred to the intent.putExtra("KEY" ... The rest of the errors in the code above were due to me guessing around at what the error was without compiler help. FYI sometimes your "preview" will scroll DOWN when it comes up - if nothing shows in your preview screen, scroll up - this is not obvious because there are no scroll bars.

2 Answers

Solved, see comment above.

hi in the Intent constructor you should pass first parameter the activity you will move from it use this keyword to refer to the current activity and the second parameter is the activity you will go to '''Intent intent = new Intent(this, FlightActivity.class);''' in '''putExtra()'''method the first parameter should be the key as String put it in double quotation and the second parameter the value you want to move it between two activity , note there is more than one '''putExtra()'''method and the difference between them in type of the second parameter you pass in ,make sure what type of parameter you passed.