java - Stored and clear array location -


i using check box using stored value in array location when select , deselect check box means clear particular array index value , store check box value @ same location.

public class mainactivity extends activity {  int m[] =new int[5]; int count=1; int t=0; checkbox chk1,chk2,chk3,chk4; int flag,increment=0; int b; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     chk1=(checkbox)findviewbyid(r.id.checkbox1);     chk2=(checkbox)findviewbyid(r.id.checkbox2);     chk3=(checkbox)findviewbyid(r.id.checkbox3);     chk4=(checkbox)findviewbyid(r.id.checkbox4);     button next=(button)findviewbyid(r.id.next);     button previous=(button)findviewbyid(r.id.previous);           chk1.setoncheckedchangelistener(new oncheckedchangelistener() {         @override         public void oncheckedchanged(compoundbutton arg0, boolean ischecked) {             // todo auto-generated method stub              if(chk1.ischecked())             {                                                        flag=1;                 m[increment]=flag;                 increment++;                 count++;                             }             else              { increment--;             count--;                      }                        }     });     chk2.setoncheckedchangelistener(new oncheckedchangelistener() {          @override         public void oncheckedchanged(compoundbutton arg0, boolean ischecked) {             // todo auto-generated method stub             if(chk2.ischecked())             {                 flag=2;                 m[increment]=flag;                 increment++;                 count++;             }             else                     {                                    increment--;                 count--;             }                        }     });     chk3.setoncheckedchangelistener(new oncheckedchangelistener() {          @override         public void oncheckedchanged(compoundbutton arg0, boolean ischecked) {             // todo auto-generated method stub              if(chk3.ischecked())             {                 flag=3;                 m[increment]=flag;                 increment++;                 count++;             }             else              {                        increment--;                 count--;             }         }     });     chk4.setoncheckedchangelistener(new oncheckedchangelistener() {          @override         public void oncheckedchanged(compoundbutton arg0, boolean ischecked) {             // todo auto-generated method stub             if(chk1.ischecked())             {                 flag=4;                 m[increment]=flag;                 increment++;                 count++;                     }             else             {                    increment--;                 count--;             }         }});     next.setonclicklistener(new onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             t++;             if(t<4)             {                 switch (t) {                 case 1:                     b=m[0];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                 case 2:                     b=m[1];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                 case 3:                     b=m[2];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                 case 4:                     b=m[3];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                               default:                     if(t<=3)                     {                         t=3;                     }                     break;                 }             }         }      });     previous.setonclicklistener(new onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             t--;             if(t>=0)             {                 switch (t) {                 case 1:                     b=m[0];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                 case 2:                     b=m[1];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                 case 3:                     b=m[2];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                 case 4:                     b=m[3];                     if(b==1)                     {                         toast.maketext(getapplicationcontext(), "one", 100).show();                     }else if(b==2)                     {                         toast.maketext(getapplicationcontext(), "two", 100).show();                     }else if(b==3)                     {                         toast.maketext(getapplicationcontext(), "three", 100).show();                     }else if(b==4)                     {                         toast.maketext(getapplicationcontext(), "four", 100).show();                     }                     break;                                default:                     if(t<=0)                     {                         t=1;                     }                     break;                 }             }         }      }); } 

logcat here

e/androidruntime(1522): fatal exception: main     e/androidruntime(1522): java.lang.arrayindexoutofboundsexception: length=5index=-1 

i think issue here:

chk4.setoncheckedchangelistener(new oncheckedchangelistener() {      @override     public void oncheckedchanged(compoundbutton arg0, boolean ischecked) {         // todo auto-generated method stub         if(chk1.ischecked())         {             flag=4;             m[increment]=flag;             increment++;             count++;                 }         else         {                increment--;             count--;         }     }}); 

there should chk4 instead of chk1 in if block.


Comments