java OutOfBoundsException

时间:2016-11-11 21:29:01

标签: java android indexoutofboundsexception

我的问题是当我点击SearchView搜索项目时我有arrayList,例如当我写pepsi时系统显示消息不幸的是,你的应用程序已被停止。

这是代码中的错误

            ImageLoader.getInstance().displayImage(ittList.get(position).getPic(), holder.ivPic1, new ImageLoadingListener() 

这是错误消息

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.abdulelahaldajani.testitem, PID: 6129
              java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
                  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                  at java.util.ArrayList.get(ArrayList.java:308)
                  at com.example.abdulelahaldajani.testitem.ShowItemsActivity$itemAdapter.getView(ShowItemsActivity.java:275)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                  at android.widget.GridView.makeAndAddView(GridView.java:1439)
                  at android.widget.GridView.makeRow(GridView.java:366)
                  at android.widget.GridView.fillDown(GridView.java:307)
                  at android.widget.GridView.fillSpecific(GridView.java:587)
                  at android.widget.GridView.layoutChildren(GridView.java:1293)
                  at android.widget.AbsListView.onLayout(AbsListView.java:2148)
                  at android.view.View.layout(View.java:16630)
                  at android.view.ViewGroup.layout(ViewGroup.java:5437)
                  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                  at android.view.View.layout(View.java:16630)
                  at android.view.ViewGroup.layout(ViewGroup.java:5437)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                  at android.view.View.layout(View.java:16630)
                  at android.view.ViewGroup.layout(ViewGroup.java:5437)
                  at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                  at android.view.View.layout(View.java:16630)
                  at android.view.ViewGroup.layout(ViewGroup.java:5437)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                  at android.view.View.layout(View.java:16630)
                  at android.view.ViewGroup.layout(ViewGroup.java:5437)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                  at android.view.View.layout(View.java:16630)
                  at android.view.ViewGroup.layout(ViewGroup.java:5437)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                  at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                  at android.view.View.layout(View.java:16630)
                  at android.view.ViewGroup.layout(ViewGroup.java:5437)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:670)

1 个答案:

答案 0 :(得分:1)

你得到的错误正如它所说的那样,列表的大小是3,而你正试图在没有第三个元素时获得第三个元素。要解决此问题,请执行

   ImageLoader.getInstance().displayImage(ittList.get(position-1).getPic(), holder.ivPic1, new ImageLoadingListener()

哪个会获得之前的元素,这可能就是你想要的。请记住,它从索引开始为0,而不是1.所以这三个元素在索引0,1和2中。