2015-10-12 6 views
0

Ошибка:Android ArrayIndexOutOfBoundsException класса View

java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 
at WeekView.getMoreEvents(WeekView.java:614) 

В классе, который расширяет View, я получаю список ид таким образом:

public void setElencoIdOperatori(int[] id_nome_op){ 
this.id_nome_op = id_nome_op; 
} 

Затем, с помощью этого метода, я должен сравните id_nome_op с eventRect.event.getIdOperatore(). Когда все то же самое, я хочу добавить событие в массив eventRects. Спасибо за вашу помощь

private void getMoreEvents(Calendar day){ 
// Get more events if the month is changed. 
if(mEventRects ==null) 
    mEventRects =newArrayList<EventRect>(); 

if(mMonthChangeListener ==null&&!isInEditMode()) 
    thrownewIllegalStateException("You must provide a MonthChangeListener"); 

// If a refresh was requested then reset some variables. 
if(mRefreshEvents){ 
    mEventRects.clear(); 
mFetchedMonths =newint[3]; 
} 

    ... 

    // Get events of this month. 
if(mFetchedMonths[1]<1||mFetchedMonths[1]!= day.get(Calendar.MONTH)+1||mRefreshEvents){ 
     if(!containsValue(lastFetchedMonth,day.get(Calendar.MONTH)+1)&&!isInEditMode()){ 
      List<WeekViewEvent> events =mMonthChangeListener.onMonthChange(day.get(Calendar.YEAR),day.get(Calendar.MONTH)+1); 
sortEvents(events); 
for(WeekViewEventevent: events){ 
       cacheEvent(event); 
} 
     } 
     mFetchedMonths[1]= day.get(Calendar.MONTH)+1; 
} 
... 

    // Prepare to calculate positions of each events. 
ArrayList<EventRect> tempEvents =newArrayList<EventRect>(mEventRects); 
mEventRects =newArrayList<EventRect>(); 

for(intj =1;j <=id_nome_op.length;){ 
     ArrayList<EventRect> eventRects =newArrayList<EventRect>(); 
for(EventRect eventRect : tempEvents){ 
      if(eventRect.event.getIdOperatore() == id_nome_op[j]) 
       eventRects.add(eventRect); 
    } 
     computePositionOfEvents(eventRects); 
j++; 

} 
} 
+0

You запустите свой самый внешний цикл 'for' в' int j = 1'. Поскольку массивы основаны на нулевом значении, это приводит к тому, что ваш индекс в id_nome_op [j] 'отключается одним. –

ответ

0

Это немного трудно понять ваш код, но я думаю, что вы, возможно, проблема здесь:

for(int j =1;j <=id_nome_op.length;) { 
    ArrayList<EventRect> eventRects =newArrayList<EventRect>(); 
    for(EventRect eventRect : tempEvents){ 
     if(eventRect.event.getIdOperatore() == id_nome_op[j]) 
      eventRects.add(eventRect); 
    } 
    computePositionOfEvents(eventRects); 
j++; 

} 

При доступе к id_nome_op[j]. Попробуйте изменить цикл для:

j < id_nome_op.length 

Как M @ Mike указал, вам также необходимо initalizing свой внешний контур для 0, как вы хотите, чтобы все элементы в массиве:

for(int j = 0; j < id_nome_op.length;) 
Смежные вопросы