简体   繁体   中英

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

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.

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. Just make them fields, and it'll be fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM