简体   繁体   English

变量'btnsave'是从内部类中访问的,需要声明为final

[英]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.

相关问题 从内部类访问变量“ ”,需要声明为 final - Variable ' ' is accessed from within inner class, needs to be declared final 从内部 class 访问的变量需要声明为 final - Variable accessed from within inner class needs to be declared final “从内部类访问变量需要声明为final”错误 - "variable is accessed from within inner class needs to be declared final" error 变量(dialogView)是从内部类中访问的,需要声明为final - Variable (dialogView) is accessed from within inner class, needs to be declared final 从内部类访问变量“i”,需要声明为 final - Variable 'i' is accessed from within inner class, needs to be declared final 在内部类中访问变量。 需要宣布为最终 - Variable is accessed within inner class. Needs to be declared final 在内部类中访问变量“名称”。 需要宣布为最终 - Variable “name” is accessed within inner class. Needs to be declared final 需要在OnTouchListener中用MediaPlayer声明内部类中访问的变量的最终声明 - Variable Is Accessed Within Inner Class Needs to be Declared Final with MediaPlayer In a OnTouchListener 在内部类中访问如何解析变量,并且需要将其声明为final - How to resolve variable is accessed within inner class and needs to be declared final 从内部类访问,需要声明为final - is accessed from within inner class, needs to be declared final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM