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 trialMartins Rokis
6,995 Pointswhere is my error?
hi, where did i go wrong here? my code doesn't compile, can't solve this challenge
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LandingActivity extends Activity {
public Button mThrustButton;
public TextView mTypeLabel;
public EditText mPassengersField;
public Spaceship mSpaceship;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing);
mThrustButton = (Button)findViewById(R.id.thrustButton);
mTypeLabel = (TextView)findViewById(R.id.typeTextView);
mPassengersField = (EditText)findViewById(R.id.passengersEditText);
// Add your code here!
mSpaceship=new Spaceship();
}
public LandingActivity(String var){
var="FIREFLY";
}
}
public class Spaceship {
private String mType;
private int mNumPassengers = 0;
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
public int getNumPassengers() {
return mNumPassengers;
}
public void setNumPassengers(int numPassengers) {
mNumPassengers = numPassengers;
}
public Spaceship() {
mType = "SHUTTLE";
}
public Spaceship(String type) {
mType = type;
}
}
2 Answers
Philip Gales
15,193 PointsChallenge number 1
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LandingActivity extends Activity {
public Button mThrustButton;
public TextView mTypeLabel;
public EditText mPassengersField;
public Spaceship mSpaceship;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing);
mThrustButton = (Button)findViewById(R.id.thrustButton);
mTypeLabel = (TextView)findViewById(R.id.typeTextView);
mPassengersField = (EditText)findViewById(R.id.passengersEditText);
// Add your code here!
mSpaceship=new Spaceship("FIREFLY");
}
}
You tried to create a new constructor. They only wanted you to pass the FIREFLY string into the Spaceship constructor.
Seth Kroger
56,413 PointsYou have two errors: First, mSpaceship=new Spaceship();
won't work because Spaceship doesn't have a default, or argument-less constructor. It does have a constructor that takes a String however. mSpaceship=new Spaceship("Some string here");
You should be able to figure out what sting value to pass in.
Second, you added a constructor to the Activity. This is something you shouldn't do in Android. Android components like Activities, Fragments, Services, etc. are managed by the OS by their lifecycle methods, and the OS takes the responsibility of constructing them through the default constructor. By supplying a constructor with arguments, you are overriding the default constructor. But because the OS has no idea how to call the new constructor or what to supply as arguments, it fails to compile because the expected constructor doesn't exist.