I am an android beginner.
I’m struggling to understand why startActivity runs properly when copied from a tutorial I found and fails when I make the smallest change.
Code from the tutorial:
private class ButtonHandler implements View.OnClickListener {
public void onClick(View v) {
handleButtonClick();
}
}
private void handleButtonClick() {
startActivity(new Intent(this, SecondAct.class));
}
That works. When I try to change it to what I would consider a simpler design, I am getting an error.
private class ButtonHandler implements View.OnClickListener {
public void onClick(View v) {
startActivity(new Intent(this, SecondAct.class));
}
}
The error is:
The constructor Intent(FirstTwoApps.ButtonHandler, Class)
is undefined
Notice that all I did was moved the action from the handleButtonClick() method to the onClick() method. Apparently that is not allowed, but I don’t understand why.
Any help is greatly appreciated.
You need to change your
thisreference to that of the enclosing class, i.e. if your class is namedMain, change it toMain.this.