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) Finishing the User Interface Using a Model in the Controller

Question on getter

I think I'm on the right track, but I may be way off.
I added a parameter to the spaceship class in spaceship.java, then tried to pull the same parameter out of Landing activity.java.
I couldn't understand the syntax errors.

LandingActivity.java
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",numPassengers);

      mTypeLabel.setText(mSpaceship.getType());
      mPassengersField.setText(mSpaceship.getnumPassengers().toString());
    }
}
Spaceship.java
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, int NumPassengers) {
      mType = type;
      mNumPassengers = numPassengers;
    }
}

2 Answers

Hi Robert,

There's not much to comment on with your code - you're pretty much there!

Here's my solution:

      mSpaceship = new Spaceship("FIREFLY");
      mTypeLabel.setText(mSpaceship.getType());
      mPassengersField.setText(mSpaceship.getNumPassengers() + "");

You don't need to mess with numPassengers in the constructor; that just takes a string. Your mTypeLabel is spot on and the mPassengersField``` piece looks OK - I need to try your code to see if that's not what the compiler is expecting. I just used a 'moosh' for a quick conversion to a string.

Hope that helps!

Steve.

Two issues in your last line. First, there's a slight typo in the method name, getnumPassengers() - that needs a capital 'N'. And it isn't liking the use of toString().

I think that's because toString() is a class method of Integer. So you would use that like Integer.toString(i).

Putting that in the code here might look like:

 mPassengersField.setText(Integer.toString(mSpaceship.getNumPassengers()));

I hope that helps!!

Steve.

Hi Ajith vishwanath ,

You have a class called MyProject that extends the Color class.

You need to extend an Activity of some sort, else th methods you're trying to use, such as super.onCreate() and findViewById() aren't methods of the class.

If you change your first line to extend a different class - the original project used ActionBarActivity but that may be deprecated now - then this should work. You don't want to extend Color. Try it with ActionBarActivity or just Activity and see what happens.

Hope that helps a little.

Steve.