java 错误:无法为最终变量赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45636551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: cannot assign a value to final variable
提问by Goji Berry
I am facing the following issue with Android Studio
我在 Android Studio 中面临以下问题
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int numeroHomem = 0;
final int numeroMulher = 0;
final int numeroPessoas = 0;
final TextView campoTexto = (TextView) findViewById(R.id.pessoas);
final Button botaoHomem = (Button) findViewById(R.id.homem);
final Button botaoMulher = (Button) findViewById(R.id.mulher);
final Button botaoReset = (Button) findViewById(R.id.reset);
botaoHomem.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
numeroHomem++;
numeroPessoas++;
String mensagem = Integer.toString(numeroPessoas);
campoTexto.setText("Total: " + mensagem + " pessoas");
botaoHomem.setText(Integer.toString(numeroHomem));
}
});
} }
error: cannot assign a value to final variable numeroHomem
error: cannot assign a value to final variable numeroPessoas
错误:无法为最终变量 numeroHomem 赋值
错误:无法为最终变量 numeroPessoas 赋值
回答by Ali
you cannot change final
variable after initialization
final
初始化后不能更改变量
what you can do is declare your variable in your class instead of onCreate()
你可以做的是在你的类中声明你的变量而不是 onCreate()
public class MainActivity extends AppCompatActivity {
int numeroHomem = 0;
int numeroMulher = 0;
int numeroPessoas = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView campoTexto = (TextView) findViewById(R.id.pessoas);
final Button botaoHomem = (Button) findViewById(R.id.homem);
final Button botaoMulher = (Button) findViewById(R.id.mulher);
final Button botaoReset = (Button) findViewById(R.id.reset);
botaoHomem.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
numeroHomem++;
numeroPessoas++;
String mensagem = Integer.toString(numeroPessoas);
campoTexto.setText("Total: " + mensagem + " pessoas");
botaoHomem.setText(Integer.toString(numeroHomem));
}
});
}
}
回答by clabe45
As your comment suggests, the message the compiler requires all the variables, used in lambdas, to be effectively final
implies that you need to set your variable to instance properties, so the compiler will know where to look.
正如您的评论所暗示的那样,该消息the compiler requires all the variables, used in lambdas, to be effectively final
暗示您需要将变量设置为实例属性,因此编译器将知道在哪里查找。
回答by EasyJoin Dev
This is not an issue of Android Studio or Android in general. Once you have declare an object as final
you can't change it. Create, for example, an utility class, declare in that class the numeroHomem
and numeroPessoas
as public static
and modify them in the onClick
method.
这通常不是 Android Studio 或 Android 的问题。一旦你声明了一个对象,final
你就不能改变它。例如,创建一个实用程序类,在该类中声明numeroHomem
和numeroPessoas
aspublic static
并在onClick
方法中修改它们。
回答by asalamon74
If you declare numeroHomem
and numeroPessoas
as final variables then you cannot increase their values later using numeroHomem++
and numeroPessoas++
. Just remove the final keywords.
如果将numeroHomem
and声明numeroPessoas
为 final 变量,则以后不能使用numeroHomem++
and增加它们的值numeroPessoas++
。只需删除最终关键字。
回答by jigar savaliya
You can not change the value of final variable, simply remove final from you variable.
您不能更改 final 变量的值,只需从变量中删除 final。
int numeroHomem = 0;
int numeroMulher = 0;
int numeroPessoas = 0;