2011-08-05 79 views
3

我剛剛得到一個奇怪的IndexOutOfBoundsException異常,我似乎無法重現。它似乎與我的列表活動有關:IndexOutOfBoundsException異常與列表視圖

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 
at java.util.ArrayList.get(ArrayList.java:311) 
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:229) 
at android.widget.AbsListView.obtainView(AbsListView.java:1410) 
at android.widget.ListView.makeAndAddView(ListView.java:1802) 
at android.widget.ListView.fillDown(ListView.java:727) 
at android.widget.ListView.fillSpecific(ListView.java:1359) 
at android.widget.ListView.layoutChildren(ListView.java:1633) 
at android.widget.AbsListView.onLayout(AbsListView.java:1263) 
at android.view.View.layout(View.java:7088) 
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 
at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 
at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 
at android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.widget.FrameLayout.onLayout(FrameLayout.java:334) 
at android.view.View.layout(View.java:7088) 
at android.view.ViewRoot.performTraversals(ViewRoot.java:1056) 
at android.view.ViewRoot.handleMessage(ViewRoot.java:1752) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
at dalvik.system.NativeStart.main(Native Method) 

我在想這可能與我如何添加/刪除腳註有關。下面是該代碼:

private void setLoadMoreFooter() { 
    resumeProgressBarAnimation(progressbar); 
    if (getListView().getFooterViewsCount() != 0 || currentCategory == null) { 
     try { 
      getListView().removeFooterView(footerView); 
     } catch (ClassCastException e) { 
     } 
    } 
    cursor = getVouchersFromDatabase(); 
    Log.d(TAG, "Determining FOOTER status:\nCurrent page size: " + cursor.getCount()%10 + "\nCurrent category: " + currentCategory + "\nPage: " + currentCategory.getPage()); 
    // Add footer if there are 10 vouchers on the current page 
    if (cursor.getCount()%10 == 0 && currentCategory != null) { 
     getListView().addFooterView(footerView, null, true); 
    } 
    else if (currentCategory.getPage() >= 2 && cursor.getCount()%10 != 0) { 
     getListView().addFooterView(footerView, null, false); 
    } 
} 

通過源代碼來看,錯誤似乎在headerviewlistadapter的getview方法在這行拋出:

return mHeaderViewInfos.get(position).view; 

有誰知道這樣做的原因可能?

+0

我們需要看到的代碼 –

+0

我們可以看到的headerviewlistadapter的代碼? – Pikaling

+0

什麼是位置和什麼是mHeaderViewInfos.size()? – CrackerJack9

回答

0

通過在谷歌網站上的代碼看後,我看到了很多不同類型的初始化的,並接入到mHeaderViewInfos,但我沒有真正看到被添加到任何東西。拋出一些調試點並追蹤它可能會有所幫助。祝你好運!

+1

感謝您的回覆。我試圖隱藏listview onstop,並使其再次onrestart可見,我從未遇到它。 –

0

使用addFooterView(footerView)這將是解決這一問題。

0

@Kevin邱提示隱藏的東西幫助。我只需要隱藏headerview,而不是整個列表。這裏是爲我工作的代碼,假定頭視圖命名爲mHeaderView
@Override public void onStop() { super.onStop(); mHeaderView.setVisibility(View.GONE);// fixes weird bug with header view }

,並確保它顯示了你切換到不同的應用程序後,執行下面的代碼。
@Override public void onStart() { if(mHeaderView.getVisibility() == View.GONE) mHeaderView.setVisibility(View.VISIBLE); super.onStart(); }

1

我解決了這個問題,確保在數據集更改時通知適配器。 在我的情況下,數據被另一個線程改變,列表視圖檢查後,如果數據是正確的。 查看listview源代碼後。我發現那位例外扔在這裏:

public View More ...getView(int position, View convertView, ViewGroup parent) { 
    // Header (negative positions will throw an ArrayIndexOutOfBoundsException) 
    int numHeaders = getHeadersCount(); 
    if (position < numHeaders) { 
     return mHeaderViewInfos.get(position).view; 
    } 

    // Adapter 
    final int adjPosition = position - numHeaders; 
    int adapterCount = 0; 
    if (mAdapter != null) { 
     adapterCount = mAdapter.getCount(); 
     if (adjPosition < adapterCount) { 
      return mAdapter.getView(adjPosition, convertView, parent); 
     } 
    } 

    // Footer (off-limits positions will throw an ArrayIndexOutOfBoundsException) 
    return mFooterViewInfos.get(adjPosition - adapterCount).view; 
} 

所以你可以看到,mFooterViewInfos.get(adjPosition - adapterCount)。查看拋出IndexOutOfBoundsException異常。因爲位置大於listview認爲它會。

你要做的就是找出引起這個異常的那個視圖。並確保一旦數據改變立即通知適配器!

+0

這幫了我,我的問題解決了,原因是數據是一個全局變量,並在網絡請求中的某個地方被改變。 (只有android 6.0報告這個bug,所以我認爲刷新機制與之前有些不同。) – herbertD

0

我遇到了同樣的問題,當使用ListView與標題視圖或頁腳視圖添加/刪除。所以嘗試了Stack Overflow的不同方法,我找到了一個解決方案來忽略這個處理dispatchDraw方法的錯誤,如下所示。

public class CustomListView extends ListView{ 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     try { 
      super.dispatchDraw(canvas); 
     } catch (IndexOutOfBoundsException e) { 
      Log.e("luna", "Ignore list view error ->" + e.toString()); 
     } 
    } 
} 

希望能幫助你。

請參考此鏈接How to debug Android java.lang.IndexOutOfBoundsException HeaderViewListAdapter.java line

樂趣@。@

相關問題