java - The final local variable cb may already have been assigned -


i trying getid in if statement check whether checkboxes set or not dont know how can define cb in if statement work.

when declare cb local variable commeted getting 2 errors:

the final local variable cb may have been assigned

cannot invoke ischecked() on primitive type int

        private void createcheckboxlist(final arraylist<integer> items) {                 final checkbox cb;                  final linearlayout ll = (linearlayout) findviewbyid(r.id.lila);                 (int = 0; < items.size(); i++) {        //here getting `the final local variable cb may have been assigned`                      cb = new checkbox(this);                     cb.settext(string.valueof(items.get(i)));                     cb.setid(i);                     ll.addview(cb);                  }                 button btn = new button(this);                 btn.setlayoutparams(new linearlayout.layoutparams(500, 150));                 btn.settext("submit");                 ll.addview(btn);                  btn.setonclicklistener(new view.onclicklistener() {                      @override                     public void onclick(view v) {                         (int : items) {                          // here getting `cannot invoke ischecked() on primitive type int         `                             if (cb.getid().ischecked()) {                              }                         }                      }                 });              } 

you've declared variable final (can't changed once set):

final checkbox cb; 

you either need set value @ point, or remove final modifier (your loop try assign value more once).

as other issue:

if (cb.getid().ischecked()) 

when add .ischecked() after .getid() it's short way of saying, "call method on returned object of first method.

the error telling method doesn't return object, primitive type (int). need call second method on checkbox object, try like:

((checkbox)v).ischecked(); 

or, if have id:

((checkbox) findviewbyid(id)).ischecked(); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -