java.lang.IndexOutOfBoundsException:索引:10,大小:10

时间:2017-07-12 04:00:27

标签: java android

请勿在阅读前将此问题标记为重复。我是java的新手,正在处理一个我正面临着

问题的应用程序
java.lang.IndexOutOfBoundsException: Index: 10, Size: 10

当我点击列表的最后一项时。我知道我的数组就像0-9,所以如果我使用10作为数字,那么我就会收到错误。我的代码如下所示

private class Page {

    private int mIndx;
    private ArrayList<HashMap<String, String>> mQuotes;


    private Page(int indx, ArrayList<HashMap<String, String>> quoteList) {
        mQuotes = quoteList;
        mIndx = indx;
    }

    private HashMap<String, String> getQuote(int indx) {

        return mQuotes.get(indx);

    }
}

此函数返回上面调用的错误。现在我试过这样的

private class Page {
    public static int postion;
    private int mIndx;
    private ArrayList<HashMap<String, String>> mQuotes;
    private Page(int indx, ArrayList<HashMap<String, String>> quoteList) {
        mQuotes = quoteList;
        mIndx = indx;
    }
    private HashMap<String, String> getQuote(int indx) {

        if(indx == mQuotes.size())
        {
            postion = indx- 1;
        }
        else{
            postion = indx;

        }
        return mQuotes.get(postion);

    }

}

这是我的报价方法

private HashMap<String, String> getQuote(int position) {
    int pageIndx = ((position + 1) / QuotesListFragment.ITEMS_PER_PAGE) + (((position + 1) % QuotesListFragment.ITEMS_PER_PAGE) > 0 ? 1 : 0);
    Page page = mQuotePages.get(pageIndx);
    if (page == null) {
        page = loadQuotePage(pageIndx);

    }
    int quoteIndxOfPage = position % QuotesListFragment.ITEMS_PER_PAGE;
    return page.getQuote(quoteIndxOfPage);

}

如果我缺少添加

的更多代码
public Cursor getQuotesByPage(int quotesType,String author,int pageIndx,int numOfItems){

        int offset=(pageIndx-1)*numOfItems;

        switch(quotesType){
            case 1:
                return  getQuotes(offset,numOfItems);
            case 2:
                return  getFavoriteQuotes(offset,numOfItems);
            case 3:
                return getAuthorQuotes(author,offset,numOfItems);
            case 4:
                return getAuthorQuotesTopLiked(author, offset, numOfItems);
            case 5:
                return getAuthorQuotesTopShared(author, offset, numOfItems);
            case 6:
                return  getQuotesTopLiked(offset,numOfItems);
            case 7:
                return  getQuotesTopShared(offset,numOfItems);

        }

        return null;

    }


    public Cursor getQuotes(int offset,int limit) {
        // Select All Query


        if (String.valueOf(offset).equals("50000")) {
            String query_count = "SELECT COUNT(" + QU_ID + ") AS count FROM "
                    + TABLE_QUOTES;
            Cursor c_count = database.rawQuery(query_count, null);
            c_count.moveToFirst();
            Integer count = c_count.getInt(c_count.getColumnIndex("count"));
            limit =count;// String.valueOf(count);

        }

        String query = "SELECT * FROM " + TABLE_QUOTES + " JOIN "
                + TABLE_AUTHORS + " ON " + QU_AUTHOR + " = " + AU_WEB_ID
                + " ORDER BY " + QU_ID + " DESC "+ " LIMIT " + offset+","+limit;
        //Log.i("query",query);

        Cursor cursor = database.rawQuery(query, null);

        cursor.moveToFirst();

        return cursor;

    }

完整日志在这里

&#13;
&#13;
Process: com.karopass.gujarati_status, PID: 6865
                                                                            java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
                                                                                at java.util.ArrayList.get(ArrayList.java:411)
                                                                                at com.karopass.gujarati_status.material.QuoteViewActivity$Page.getQuote(QuoteViewActivity.java:703)
                                                                                at com.karopass.gujarati_status.material.QuoteViewActivity$Page.access$1000(QuoteViewActivity.java:690)
                                                                                at com.karopass.gujarati_status.material.QuoteViewActivity.getQuote(QuoteViewActivity.java:653)
                                                                                at com.karopass.gujarati_status.material.QuoteViewActivity.access$800(QuoteViewActivity.java:48)
                                                                                at com.karopass.gujarati_status.material.QuoteViewActivity$QuoteFragmentAdapter.getItem(QuoteViewActivity.java:632)
                                                                                at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:109)
                                                                                at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1248)
                                                                                at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                                                                                at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:911)
                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
                                                                                at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                                                                                at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                                                                                at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
                                                                                at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                                                                                at android.view.View.measure(View.java:19965)
                                                                                at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2309)
                                                                                at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1392)
                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1645)
                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1280)
                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6432)
07-12 09:23:54.208 6865-6865/com.karopass.gujarati_status E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                                at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6290)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
&#13;
&#13;
&#13;

但是现在两次显示我的最后一项......我在某个地方错了......请帮助我解决问题....我能做些什么来解决这个难题......我读了很多stackflow中的类似问题但无法修复它。 感谢

3 个答案:

答案 0 :(得分:-1)

如果大小为10,则表示您可以从0到9个位置进行迭代。 (总共有10个元素。)

由于这个原因,它打印了最后一个元素2次。

  

当indx为9时(指向hashmap的最后一个元素),它将进入else条件并返回最后一个元素。

     

下次当索引为10时,它将进入if情况并且你正在执行 postion = indx- 1; ,这也将返回位置9上的元素。

答案 1 :(得分:-2)

  

指数:10,大小:10

索引从零开始枚举,所以当size为10时,max allowed index为9,因此这一行:

if(indx == mQuotes.size())

错误,因为它应该是

if(indx == mQuotes.size()-1)

答案 2 :(得分:-2)

我认为你在某种循环中调用Page类的getQuote()函数,所以你的索引参数应该始终具有从0到mQuotes.size()的值 - 1然后你可以简单地做下方。

private HashMap<String, String> getQuote(int indx) { if(indx>= mQuotes.size()) { return null;} else{return mQuotes.get(indx); }}