java - The final local variable cannot be assigned, since it is defined in an enclosing type? -


looking able create variable @ end has value time , cost display in textview eventually. running issue 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 is, "the final local variable intcost cannot assigned, since defined in enclosing type."

what mean, , there way i'd able change variable using of checkboxes?

the cause of problem final variables should initialized when declared , cannot reassigned. best bet case create inner field in anonymous class start final variables values, , inner fields 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:


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -