이것은 처음 자바 프로그래밍이기 때문에 왜 함수 밖의 변수에 액세스 할 수 없는지 이해할 수 없습니다. 나는 마지막에 변수를 변경하려고하지만 오류가 여전히있다 :Java에서 내부 클래스 내부의 최종 변수 번호에 어떻게 액세스합니까?
String[] number = this.nextNumber();
Button submitbutton = (Button)findViewById(R.id.button1);
submitbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
//Toast.makeText(Addition.this, inp.getText(), Toast.LENGTH_SHORT).show();
if(inp.getText().toString().equals("")) {
Toast.makeText(Addition.this, "Please enter your answer", Toast.LENGTH_SHORT).show();
} else {
int answer = Integer.parseInt(inp.getText().toString());
if(Integer.parseInt(number[1]) == answer) {
Toast.makeText(Addition.this, "NICE!", Toast.LENGTH_SHORT).show();
int currentPoint = Integer.parseInt((String) points.getText());
currentPoint = currentPoint + 1;
points.setText(Integer.toString(currentPoint));
number = Addition.this.nextNumber(); // this part doesn't work. why?
} else {
Toast.makeText(Addition.this, "Wrong!", Toast.LENGTH_SHORT).show();
}
}
}
});
:
여기
Cannot refer to a non-final variable number inside an inner class defined in a different method
내 코드입니다 :
The final local variable number cannot be assigned, since it is defined in an enclosing type
변수에는 final 키워드가 없습니다, 오류이됩니다
작동하지 않는 부분에 댓글을 달았습니다.
예를 들어
number
경우 배열 (String[]
는) 다음 다음이 작동하지하는String
이었다. 내 버튼을 클릭 할 때 변수의 값을 변경하는 다른 방법이 있습니까? –@ Kevin :하지만 그것은 * local 변수입니다. 그것을 바꾸면 어떤 효과가있을 것이라고 생각하십니까? –