简体   繁体   English

如何删除java.lang.IndexOutOfBoundsException?

[英]How to remove java.lang.IndexOutOfBoundsException?

I'm new in android app development and I am developing an android app. 我是android应用程序开发的新手,正在开发android应用程序。 My app uses array-lists for check-boxes and pending intents. 我的应用程序将数组列表用于复选框和未决意图。 But on execution it is giving the following error on the logcat window: 但是在执行时,它在logcat窗口中给出以下错误:

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 . 这是因为Java中的索引以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 因此,一开始,当列表为空时,当您尝试使用以下命令在索引1 add (在这种情况下,其工作方式类似于插入)

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 . 您将获得IndexOutOfBoundsException ,因为没有索引1 ,列表为空,并且只有索引0可以插入内容,因为Java中的列表索引以0开头。

Then you need to change other code to not do unnecessary i+1 like here 然后,您需要更改其他代码以免做不必要的i+1例如这里

  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 . 尽管您以60的容量初始化ArrayList l ,但其大小仍为0。因此,如docs中所述,无法在索引1处添加元素。

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. 列表索引始终以0开头,而不是以1开头。如果确实需要从1开始计数,则可以先添加一个空元素。

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. 您必须从索引0开始而不是从索引1开始。这就是Java给出错误消息的原因,因为索引0从未被初始化,因此索引1不存在。

    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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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