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 trialHenry Antonio Ambicho Trujillo
Courses Plus Student 686 PointsI don't understan why do you ask me the constructor, because , I have create this.
My MainActivityPresenter have a constructor, but nevertheless the error persiste
public class MainActivity extends AppCompatActivity implements MainActivityView{
TextView counter;
Button button;
MainActivityPresenter presenter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenter = new MainActivityPresenter(this);
counter = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int count = Integer.parseInt(counter.getText().toString());
presenter.incrementValue(count);
}
});
}
@Override
public void incrementNumber(int valor) {
counter.setText(Integer.toString(valor + 1));
}
}
public interface MainActivityView {
public void incrementNumber(int valor);
}
public class MainActivityPresenter {
MainActivity view;
public MainActivityPresenter(MainActivity view) {
this.view = view;
}
public void incrementValue(int value) {
view.incrementNumber(value);
}
}
1 Answer
Rares Conea
Courses Plus Student 15,000 PointsHi,
You can not call incrementValue() on an reference to an object of MainActivity type because the compiler does not know that MainActivity implements MainActivityView and that it also has that method. In your Presenter the field where you want to store the value received in the constructor should be of type MainActivityView, the same type should be also the value received.
MainActivityView view;
public MainActivityPresenter(MainActivityView view) {
this.view = view;
}