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

Sena Sari
Sena Sari
5,732 Points

LandingActivity interacts with the Spaceship model object we created. Start by setting spaceship to a new Spaceship

Couldnt solve this

LandingActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LandingActivity extends AppCompatActivity {

    public Button thrustButton;
    public TextView typeLabel;
    public EditText passengersField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing);

        thrustButton = (Button)findViewById(R.id.thrustButton);
        typeLabel = (TextView)findViewById(R.id.typeTextView);
        passengersField = (EditText)findViewById(R.id.passengersEditText);

        // Add your code here!
        Spaceship spaceship= new Spaceship("FIREFLY");
    }
}
Spaceship.java
public class Spaceship {
    private String shipType;
    private int numPassengers = 0;

    public String getShipType() {
      return shipType;
    }

    public void setShipType(String shipType) {
      this.shipType = shipType;
    }

    public int getNumPassengers() {
      return numPassengers;
    }

    public void setNumPassengers(int numPassengers) {
      this.numPassengers = numPassengers;
    }

    public Spaceship() {
      shipType = "SHUTTLE";
    }

    public Spaceship(String shipType) {
      this.shipType = shipType;
    }
}

2 Answers

Hi there,

This is a simple fix. Because the variable called spaceship has already been defined as being of type Spaceship you don't need to take that step again. So, delete the first Spaceship datatype.

Otherwise, you're declaring a new spaceship inside onCreate. This will exist only in onCreate so when the challenge tests the value of the member variable of the class, it'll not be set to anything.

I hope that makes sense!

Change your line of code to:

spaceship = new Spaceship("FIREFLY");

And that'll work fine.

Steve.

Sena Sari
Sena Sari
5,732 Points

Thanks for the answer it worked out well

:+1: