Android indexoutofboundsexception invalid index 1 size is 1

Android java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1

i’ve just recived this crash log but can’t understand what its caused from. Basically my app is a text editor. I have a TextWatcher on the TextView but i’m not sure if its that the problem, since the logcat dosent contain any line of my app.

3 Answers 3

I’m not sure if this can help someone else, but to fix this bug I’ve done this:

I have an EditText, with a TextWatcher.

The error is raised by this set:

To solve this I had to first remove the listener, set the text to the empty string, and then re-add the listener.

Hope it can help someone else as well.

seems this happens if you edit the attached state of a text watcher in the text watcher itself. e.g., i see this happen if i remove the textwatcher in a watcher method itself.

to verify, move the contents of onTextChanged to afterTextChanged, and see if the new stacktrace is triggered from TextView$ChangeWatcher.afterTextChanged instead.

to work around it, move that work to a handler posted from the method, or similar.

So late, but for anyone can be useful:

Check if your EditText has the textAllCaps=»true» attribute and remove it if needed.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.15.43318

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 #24

Comments

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at in.srain.cube.views.GridViewWithHeaderAndFooter$HeaderViewGridAdapter.getView(GridViewWithHeaderAndFooter.java:714)

05-12 10:49:30.335 19743-19743/com.pheth.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mingqihui.android, PID: 19743
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6588)
at android.widget.GridView.layoutChildren(GridView.java:1256)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at com.pheth.android.module.pulltorefresh.view2.PullToRefreshLayout.onLayout(PullToRefreshLayout.java:560)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

The text was updated successfully, but these errors were encountered:

Any related code?

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at in.srain.cube.views.GridViewWithHeaderAndFooter$HeaderViewGridAdapter.getView(GridViewWithHeaderAndFooter.java:713)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.GridView.makeAndAddView(GridView.java:1433)
at android.widget.GridView.makeRow(GridView.java:361)
at android.widget.GridView.fillDown(GridView.java:302)
at android.widget.GridView.fillSpecific(GridView.java:582)
at android.widget.GridView.layoutChildren(GridView.java:1310)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3838)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3637)
at android.view.View.dispatchTouchEvent(View.java:8471)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at com.pheth.android.module.pulltorefresh.view2.PullToRefreshLayout.dispatchTouchEvent(PullToRefreshLayout.java:465)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
at android.view.View.dispatchPointerEvent(View.java:8666)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)

Add head and tail, the head frequent Click the button to switch gridview content size.
Occasionally,i do not know why

© 2023 GitHub, Inc.

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Источник

Understand Arraylist IndexOutOfBoundsException in Android

I get a lot of IndexOutOfBoundsException from the any Arraylist I use. Most of the times it works fine but sometimes I get this annoying error on Arraylists i use on my project.

The main cause is always either

Help me understand the main cause of this error as no matter how many answers I’ve searched they don’t completely help me.

5 Answers 5

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 3, size is 3

It means you have ArrayList which having 3 elements where you can get each element like 0,1,2 positions. And you are trying to read 4th element which does not exists into ArrayList.

java.util.ArrayList.throwIndexOutOfBoundsException: Invalid index 0, size is 0

It means you have a empty ArrayList, and you are trying to read 1st element.

ArrayIndexOutOfBoundsException — Examples

An array-index out of bounds exception is a Java exception thrown due to the fact that the program is trying to access an element at a position that is outside an array limit, hence the words «Out of bounds». In other words, the program is trying to access an element at an index that is outside the array bounds. To understand array bounds, let us consider the following diagram:

The picture above contains an array that consists of 7 elements. Each element in the array has its own index/position. In Java, an index always starts with 0 and ends with the length of the array -1. For example, the array above consists of 7 elements, therefore it’s indices start from 0 and end with 6 (7-1). Attempting to access an element with an index less than 0 or more than 6 will cause Java to throw an ArrayIndexOutOfBoundsException.

Источник

Android Adapter «java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4»

I have problem with deleting item’s from ArrayList and synchronising Adapter . I have my RecyclerView adapter with some ArrayList inside it called items . I download some list from the server and dispaly inside it. Whenever I click on some of list items I would like to delete it from server, from local ArrayList and notify the adapter about it. The problem is that when I delete everything from down to up from the list everything is ok, but when f.e. I delete 1st element from the list and then randomly some of the elements it deletes element after the one I clicked. In some cases the app crashes (f.e. I delete 1st element then the last one). The error I get is f.e.:

java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4

Look like it’s something with list size but i don’t know what is wrong?

Here is the function where I got position from ( setPopUpListener(popupMenu, position) ):

Here is setPopUpListener() — just look at removeFromFavourites(position) :

Here is where the error appears ( removeFromFavourites(position) ):

4 Answers 4

SOLUTION — HOPE THIS WILL HELP SOMEBODY

I have found the soution for this. If somebody will ever try to dynamicly remove elements from his array list and notifyItemRemoved(position) do not send clicked position as a parameter inside onBindViewHolder(ViewHolder holder, int position) . You will meet with exactly the same situation as I did.

If you have 4 displayed elements in a list f.e. [0, 1, 2, 3] and try to remove from the end of the list everything will be fine cause clicked positions will match exactly the same positions in ArrayList . For example if you click 4th element:

position = 3 — position you will get when clicked on list element; myArray.remove(position) — will remove element with index = 3 and notifyItemRemoved(position) — will animate the list and remove deleted element from the displayed list. You are going to have following list: [0, 1, 2] . This is fine.

Situation changes when you want to delete random element. Let’s say I want to delete 3rd displayed list element. I click on it to delete so i get:

position = 2 -> myArray.remove(position) -> notifyItemRemoved(position)

In this case the ArrayList I am going to get will be like this: [0, 1, 3] . In I now click on the last dispalyed element and would like to delete it that’s what I will get:

position = 3 -> myArray.remove(position) -> notifyItemRemoved(position)

But what happens? App suddenly crashes with exception: java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3 . It means that we are trying to get element at the position that does not exist. But why? I got my clicked position from element. This is what happened:

At the beggining we had:

ARRAY LIST INDEXES -> [0, 1, 2, 3]

POSITIONS FROM CLICK -> [0, 1, 2, 3]

After Deleting 3rd element:

ARRAY LIST INDEXES -> [0, 1, 2]

POSITIONS FROM CLICK -> [0, 1, 3]

Now when I try to delete element at position = 3 we can’t do that. We do not have that position. The max position we can get is 2 . Thats why we get the exception. How to manage that problem?

Источник

Adblock
detector