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

Derrick Shepherd
Derrick Shepherd
4,861 Points

Build an Interactive Story app - Code challenge

The Assignment:

"The Activity below is used to launch a spacecraft. Tapping on the button takes the user to a new Activity where s/he can control the ship. In the 'onClick()' method, add an Intent that we can use to start 'FlightActivity'."

                   ---------------------------------------

Seems easy enough, but I'm not getting it correct for some reason, lol. Can someone give me a hint or mode of thinking without actually giving me the code? thanks. (my code is between the arrows).

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) { 
       Intent intent = new Intent(this, FlightActivity.class);
        startActivity(intent);
      }
    });
  }
}
aakarshrestha
aakarshrestha
6,509 Points

Your code looks pretty much correct. What's the problem you see?

Happy coding!

6 Answers

The only thing I can see that might not be right there is the context - I'm not sure that this alone is sufficient. You may need to specify the Activity too.

Steve.

Derrick Shepherd
Derrick Shepherd
4,861 Points

Thanks Steve, That worked! I tried that before and it didn't work because I had "MainActivity.this" instead of "LaunchActivity.this" so I thought just writing "this" will do the trick, lol. But your answer is correct, Thanks a bunch, The challenge is completed.

No problem - glad it worked!

Steve.

This worked for me!!


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) { 
   Intent intent = new Intent( LaunchActivity.this , FlightActivity.class);
    startActivity(intent);
  }
});

} }

Derrick Shepherd
Derrick Shepherd
4,861 Points

I got it working Ryan, Your code is in line with the answer Steve H. gave me. Thank s for responding my friend

Sexual Potatoes
Sexual Potatoes
12,051 Points

He teaches the students in the video to use just this. It's misleading.

Derrick Shepherd
Derrick Shepherd
4,861 Points

Thanks For Responding AAKAR, Steve Hunter's Solution worked...

guyz mine is giving an error on the id of the button

JavaTester.java:71: error: cannot find symbol activity.launchButton.mOnClickListener.onClick(null); ^ symbol: variable launchButton location: variable activity of type LaunchActivity 1 error

Hi Terrence,

Have you received an answer to this on a different thread or is this still awaiting a solution?

Steve.

Hie Steve.. i have figured it out ...thnx