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 Starting a New Activity

Kevin Gonzales
Kevin Gonzales
4,638 Points

Stuck

I cant get passed this. I created an intent inside a method and attempted to start the method that will send us to the LaunchActivity but i get errors.

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;

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

    mLaunchButton = (Button)findViewById(R.id.launchButton);
    mLaunchButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // Add your code in here!
        Start();
      }
    });
    private void Start(){
    Intent i = new Intent(this, FlightActivity.class);
      startActivity(i);
    }
  }
}
Joseph Bogaert
Joseph Bogaert
10,505 Points

Hello! So you're definitely on the right track, you have all the right information, just in a place where your onClick method can't use it I believe. To make this code work, we just have to change 2 things, Change this:

public void onClick(View v) {
        // Add your code in here!
        Start();
      }
    });
    private void Start(){
    Intent i = new Intent(this, FlightActivity.class);
      startActivity(i);
    }
  }
}

To this:

public void onClick(View v) {
                // Add your code in here!
                Start();
            }

            private void Start(){
                Intent i = new Intent(this, FlightActivity.class);
                startActivity(i);
        };

    });
}
}

And then this line:

 Intent i = new Intent(this, FlightActivity.class);

To this line, just adding a reference to the context, now that this method is solely within the onClick method.

Intent i = new Intent(LaunchActivity.this, FlightActivity.class);

I think this should give you the result you need, let me know if it works for you :)

Kevin Gonzales
Kevin Gonzales
4,638 Points

Actually all i changed first was adding a semicolon and then it passed me lol but its odd since i dont need the ; for android studio. Thank you for your help!