[英]Variable 'btnsave' is accessed from within inner class, needs to be declared final
I have the above error on my code. 我的代码上面有上述错误。 I have tried fixing this by declaring my btnsave as final, but then I get two other errors saying: 我试图通过宣布我的btnsave为最终来解决这个问题,但后来我又得到了两个错误:
Unknown class: 'btnsave' 未知类:'btnsave'
Not a statement 不是声明
Any idea how I fix this error? 知道我如何解决这个错误吗?
btnsave = (Button)findViewById(R.id.button);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int clicks = 0;
clicks++;
if (clicks >= 5){
btnsave.setEnabled(false);
}
SharedPreferences prefs = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("clicks", clicks);
editor.apply();
}
});
Two options. 两种选择。 You can declare your button as finale in this way: 您可以通过以下方式将按钮声明为结尾:
final Button btnSave;
Or you can declare your button as an attribute of the class (Activity, Fragment or whatever) this way: 或者你可以用这种方式将你的按钮声明为类的属性(Activity,Fragment或其他):
private Button btnSave;
Original: 原版的:
if (clicks >= 5){
btnsave.setEnabled(false);
}
Changed working code: 改变了工作代码:
if (clicks >= 5){
((Button)view).setEnabled(false)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.