最终局部变量不能分配,因为它是在一个封闭类型定义? [英] The final local variable cannot be assigned, since it is defined in an enclosing type?

查看:1036
本文介绍了最终局部变量不能分配,因为它是在一个封闭类型定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

展望也能在有时间和成本的价值,我会在TextView中最终显示最终创建一个变量。运行到我的整数,虽然是一个问题。

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. 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));

        }

    });
}

}

我运行到该错误,最终的局部变量intCost不能分配,因为它是在一个封闭的类型来定义。

The error I'm running into is, "The final local variable intCost cannot be assigned, since it is defined in an enclosing type."

这是什么意思,是没有什么办法,我可以改变使用所有复选框这个变量?

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. 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.

例如:

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;
        }
    }
});

更多信息:

  • Java tutorials. Anonymous class

这篇关于最终局部变量不能分配,因为它是在一个封闭类型定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆