I have a problem with a pice of code:
// Feld 1 OnClick int punkte;
int punkte;
punkte=0;
TextView counter = (TextView) findViewById(R.id.counter);
counter.setText(Integer.toString(punkte));
TextView next = (TextView) findViewById(R.id.Farbent1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
punkte=punkte+1;
};
});
When I ask eclipse it says that I have to do my “punkte” in the public void onClick(View View) to final int. But when it is final I cant change the variable
I can’t see the reason why I have to do my variable final. And is there a solution to change my variable in onClick(View View)?
best regrads
nameless
In order to refer to a local variable inside of a callback (the onClick() method) that variable has to be declared “final” so that the compiler knows that the reference to it won’t change.
You can work around this easily by making punkte a class-level field.