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 trialDerrick Shepherd
4,861 PointsBuild 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).
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);
}
});
}
}
6 Answers
Steve Hunter
57,712 PointsThe 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
4,861 PointsThanks 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.
Steve Hunter
57,712 PointsNo problem - glad it worked!
Steve.
TheBigoso /\
3,217 PointsThis 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
4,861 PointsI got it working Ryan, Your code is in line with the answer Steve H. gave me. Thank s for responding my friend
Sexual Potatoes
12,051 PointsHe teaches the students in the video to use just this. It's misleading.
Derrick Shepherd
4,861 PointsThanks For Responding AAKAR, Steve Hunter's Solution worked...
TERRENCE MAVHUNGA
6,053 Pointsguyz 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
Steve Hunter
57,712 PointsHi Terrence,
Have you received an answer to this on a different thread or is this still awaiting a solution?
Steve.
TERRENCE MAVHUNGA
6,053 PointsHie Steve.. i have figured it out ...thnx
aakarshrestha
6,509 Pointsaakarshrestha
6,509 PointsYour code looks pretty much correct. What's the problem you see?
Happy coding!