简体   繁体   中英

How to remove java.lang.IndexOutOfBoundsException?

I'm new in android app development and I am developing an android app. My app uses array-lists for check-boxes and pending intents. But on execution it is giving the following error on the logcat window:

java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0

My partial code is as follows:

public class Labs extends Activity {


    ArrayList <CheckBox> l= new ArrayList <CheckBox>(60);


    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_labs);
    addenable();


    l.add(1,(CheckBox)findViewById(R.id.checkBox101));
    l.add(2,(CheckBox)findViewById(R.id.checkBox102));
    l.add(3,(CheckBox)findViewById(R.id.checkBox103));
    l.add(4,(CheckBox)findViewById(R.id.checkBox104));
    l.add(5,(CheckBox)findViewById(R.id.checkBox105));
    l.add(6,(CheckBox)findViewById(R.id.checkBox106));
    l.add(7,(CheckBox)findViewById(R.id.checkBox107));
    l.add(8,(CheckBox)findViewById(R.id.checkBox108));
    l.add(9,(CheckBox)findViewById(R.id.checkBox109));
    l.add(10,(CheckBox)findViewById(R.id.checkBox110));
    l.add(11,(CheckBox)findViewById(R.id.checkBox111));
    l.add(12,(CheckBox)findViewById(R.id.checkBox112));
    l.add(13,(CheckBox)findViewById(R.id.checkBox113));
    l.add(14,(CheckBox)findViewById(R.id.checkBox114));
    l.add(15,(CheckBox)findViewById(R.id.checkBox115));
    l.add(16,(CheckBox)findViewById(R.id.checkBox116));
    l.add(17,(CheckBox)findViewById(R.id.checkBox117));
    l.add(18,(CheckBox)findViewById(R.id.checkBox118));
    l.add(19,(CheckBox)findViewById(R.id.checkBox119));
    l.add(20,(CheckBox)findViewById(R.id.checkBox120));
    l.add(21,(CheckBox)findViewById(R.id.checkBox121));
    l.add(22,(CheckBox)findViewById(R.id.checkBox122));
    l.add(23,(CheckBox)findViewById(R.id.checkBox123));
    l.add(24,(CheckBox)findViewById(R.id.checkBox124));
    l.add(25,(CheckBox)findViewById(R.id.checkBox125));
    l.add(26,(CheckBox)findViewById(R.id.checkBox126));
    l.add(27,(CheckBox)findViewById(R.id.checkBox127));
    l.add(28,(CheckBox)findViewById(R.id.checkBox128));
    l.add(29,(CheckBox)findViewById(R.id.checkBox129));
    l.add(30,(CheckBox)findViewById(R.id.checkBox130));
    l.add(31,(CheckBox)findViewById(R.id.checkBox131));
    l.add(32,(CheckBox)findViewById(R.id.checkBox132));
    l.add(33,(CheckBox)findViewById(R.id.checkBox133));
    l.add(34,(CheckBox)findViewById(R.id.checkBox134));
    l.add(35,(CheckBox)findViewById(R.id.checkBox135));
    l.add(36,(CheckBox)findViewById(R.id.checkBox136));
    l.add(37,(CheckBox)findViewById(R.id.checkBox137));
    l.add(38,(CheckBox)findViewById(R.id.checkBox138));
    l.add(39,(CheckBox)findViewById(R.id.checkBox139));
    l.add(40,(CheckBox)findViewById(R.id.checkBox140));
    l.add(41,(CheckBox)findViewById(R.id.checkBox141));
    l.add(42,(CheckBox)findViewById(R.id.checkBox142));
    l.add(43,(CheckBox)findViewById(R.id.checkBox143));
    l.add(44,(CheckBox)findViewById(R.id.checkBox144));
    l.add(45,(CheckBox)findViewById(R.id.checkBox145));
    l.add(46,(CheckBox)findViewById(R.id.checkBox146));
    l.add(47,(CheckBox)findViewById(R.id.checkBox147));
    l.add(48,(CheckBox)findViewById(R.id.checkBox148));
    l.add(49,(CheckBox)findViewById(R.id.checkBox149));
    l.add(50,(CheckBox)findViewById(R.id.checkBox150));
    l.add(51,(CheckBox)findViewById(R.id.checkBox151));
    l.add(52,(CheckBox)findViewById(R.id.checkBox152));
    l.add(53,(CheckBox)findViewById(R.id.checkBox153));
    l.add(54,(CheckBox)findViewById(R.id.checkBox154));
    l.add(55,(CheckBox)findViewById(R.id.checkBox155));
    l.add(56,(CheckBox)findViewById(R.id.checkBox156));
    l.add(57,(CheckBox)findViewById(R.id.checkBox157));
    l.add(58,(CheckBox)findViewById(R.id.checkBox158));
    l.add(59,(CheckBox)findViewById(R.id.checkBox159));
    l.add(60,(CheckBox)findViewById(R.id.checkBox160));

        }





    //Intents
    Intent setVibration = new Intent();
    setVibration.setClass(this, AlarmReciever.class);

    Intent setNormal = new Intent();
    setNormal.setClass(this, RingerMode.class);


    //PENDING INTENTS

    ArrayList <PendingIntent> Lab_V= new ArrayList <PendingIntent>(60);
    ArrayList <PendingIntent> Lab_N= new ArrayList <PendingIntent>(60);

    for(int i=0; i<60; i++)
    {
        Lab_V.add(i+1, PendingIntent.getBroadcast(this, i+1, setVibration,
                PendingIntent.FLAG_UPDATE_CURRENT));
        Lab_N.add(i+1, PendingIntent.getBroadcast(this, i+1, setNormal,
                PendingIntent.FLAG_UPDATE_CURRENT));
    }


    // create the object
    AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);


    for(int i=0; i<60; i++)
    {

        if(l.get(i+1).isChecked()){

             mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, L[i+1].getTimeInMillis(), 7*24*60*60*1000, Lab_V.get(i+1));
             mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, L[i+1].getTimeInMillis()+50*60*1000, 7*24*60*60*1000, Lab_N.get(i+1));


        }
    }

    }

Please help me resolve this error. It'll be a huge help. Thanks in advance.

Add elements like this:

l.add((CheckBox)findViewById(R.id.checkBox101));
l.add((CheckBox)findViewById(R.id.checkBox102));
l.add((CheckBox)findViewById(R.id.checkBox103));
[..]

or

l.add(0, (CheckBox)findViewById(R.id.checkBox101));
l.add(1, (CheckBox)findViewById(R.id.checkBox102));
l.add(2, (CheckBox)findViewById(R.id.checkBox103));
[..]

or least tedious way

for (int i = 101; i <= 160; i++) {
   String viewId = "checkBox" + i;
   l.add((CheckBox) findViewById(getResources().getIdentifier(viewId, "id", getPackageName())));
}

It's because indexes in Java start with 0 . So at the beginning, when the list is empty, when you try to add (in that case it works like insert) item at index 1 using

l.add(1,(CheckBox)findViewById(R.id.checkBox101));

you get IndexOutOfBoundsException , because there is no index 1 , the list is empty, and there is only index 0 where you can insert something, because list indexes in Java start with 0 .

Then you need to change other code to not do unnecessary i+1 like here

  for(int i=0; i<60; i++)
    {
        if(l.get(i+1).isChecked()){
            [...]
        }
    }

Now it should be just:

  for(int i=0; i<60; i++)
    {
        if(l.get(i).isChecked()){
            [...]
        }
    }

As you did not provide the whole source code and did not post the stack trace, this answer is based on assumptions.

Although you initialize the ArrayList l with a capacity of 60, its size is still 0. Adding an element at index 1 is therefore impossible, as stated in the docs .

List indices always start with 0, not with 1. If you really need to start counting with 1, you can add an empty element first.

You have to start at index 0 not index 1. That's why java is giving a error saying index 1 does not exist because index 0 was never initialized.

    l.add(0,(CheckBox)findViewById(R.id.checkBox101));
    l.add(1,(CheckBox)findViewById(R.id.checkBox102));
    .
    .
    .
    l.add(58,(CheckBox)findViewById(R.id.checkBox101));
    l.add(59,(CheckBox)findViewById(R.id.checkBox102));

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