[英]The final local variable cannot be assigned, since it is defined in an enclosing type?
Looking to be able to create a variable at the end that has a value for Time and Cost that I will display in an TextView eventually. 希望能够在最后创建一个变量,该变量具有时间和成本的值,最终将在TextView中显示该值。 Running into an issue with my ints though. 虽然遇到了我的问题。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int intTime = 30;
final int intCost = 10;
CheckBox CP = (CheckBox)findViewById(R.id.checkPepperoni);
CheckBox CS = (CheckBox)findViewById(R.id.checkSausage);
CheckBox CB = (CheckBox)findViewById(R.id.checkBacon);
CheckBox CM = (CheckBox)findViewById(R.id.checkMushroom);
final ImageView cheese =(ImageView)findViewById(R.id.imgCheese);
final ImageView pepperoni =(ImageView)findViewById(R.id.imgPepperoni);
final ImageView sausage =(ImageView)findViewById(R.id.imgSausage);
final ImageView bacon =(ImageView)findViewById(R.id.imgBacon);
final ImageView mushroom =(ImageView)findViewById(R.id.imgMushroom);
final SharedPreferences sharedPref =PreferenceManager.getDefaultSharedPreferences(this);
CP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
//Pepperoni Listener
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked1) {
// TODO Auto-generated method stub
if(isChecked1){
pepperoni.setImageResource(R.drawable.pepperoni);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
pepperoni.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
CS.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
//Sausage Listener
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked2) {
// TODO Auto-generated method stub
if(isChecked2){
sausage.setImageResource(R.drawable.sausage);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
sausage.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
CB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked3) {
// TODO Auto-generated method stub
if(isChecked3){
bacon.setImageResource(R.drawable.bacon);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
bacon.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
CM.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked4) {
// TODO Auto-generated method stub
if(isChecked4){
mushroom.setImageResource(R.drawable.mushroom);
intTime = intTime + 6;
intCost = intCost + 5;
}
else{
mushroom.setImageResource(0);
intTime = intTime - 6;
intCost = intCost - 5;
}
}});
ImageButton IB = (ImageButton)findViewById(R.id.imgBtnGo);
IB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String time = Integer.toString(intTime);
String cost = Integer.toString(intCost);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("keyTime", time);
editor.putString("keyCost", cost);
startActivity(new Intent(MainActivity.this, ResultActivity.class));
}
});
}
}
The error I'm running into is, "The final local variable intCost cannot be assigned, since it is defined in an enclosing type." 我遇到的错误是“无法分配最终的局部变量intCost,因为它是用封闭类型定义的。”
What does this mean, and is there any way that I'd be able to change this variable using all of the checkboxes? 这是什么意思,有什么办法可以使用所有复选框更改此变量?
The cause of the problem is that final
variables should be initialized when declared and cannot be reassigned . 问题的原因是final
变量在声明时应初始化,并且不能重新分配 。 The best bet for this case will be to create inner field in the anonymous class that start with the final variables values, and this inner fields are updated periodically. 在这种情况下,最好的选择是在匿名类中创建以最终变量值开头的内部字段,并且该内部字段会定期更新。
Example: 例:
CP.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//Pepperoni Listener
int innerTime = intTime;
int innerCost = intCost;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked1) {
if(isChecked1) {
pepperoni.setImageResource(R.drawable.pepperoni);
innerTime += 6;
innerCost += 5;
} else {
pepperoni.setImageResource(0);
innerTime -= 6;
innerCost -= 5;
}
}
});
More info: 更多信息:
I can't why are you using midficator final
in first place, your code will work fine if you just remove it, i'm not sure, but i've checked for onClick
and it's work. 我不知道为什么要首先使用midficator final
,如果您只是删除它,您的代码将运行良好,我不确定,但是我已经检查过onClick
并且可以正常工作。 Just make them fields, and it'll be fine 只需将它们设置为字段,就可以了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.