Project: titanium
Issue TIMOB-20116: Android - ScrollableView removeView(): java.lang.IndexOutOfBoundsException: Invalid index X, size is X
This is happening only in the android platform. Seems like when you call the ScrollableView.removeView() method it automatically jumps to the previous view if you're currently viewing the one you're trying to delete. However, that fires an exception. How to reproduce: * Create an Android-enabled project with a single, main view. * Add to that view a ScrollableView and a Button below it. * Add 3 empty views to the ScrollableView. * Add an event to the button which fires the following code: {code:javascript} _.each($.scrollableView.getViews(), function(view) { $.scrollableView.removeView(view); }); {code} * Click the button with the view number 1 focused: it will work. * Reopen the app (so the views get added again), browse to the 3rd view and click the button. It will throw this exception: {code:java} [ERROR] : TiApplication: (main) [47704,48391] Sending event: exception on thread: main msg:java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1; Titanium 5.0.3,2015/10/02 00:31,d4496fe [ERROR] : TiApplication: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 [ERROR] : TiApplication: java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) [ERROR] : TiApplication: java.util.ArrayList.get(ArrayList.java:308) [ERROR] : TiApplication: ti.modules.titanium.ui.widget.TiUIScrollableView$2.onPageSelected(TiUIScrollableView.java:159) [ERROR] : TiApplication: android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:578) [ERROR] : TiApplication: android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555) [ERROR] : TiApplication: android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513) [ERROR] : TiApplication: android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:912) [ERROR] : TiApplication: android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2826) [ERROR] : TiApplication: android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) [ERROR] : TiApplication: android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276) [ERROR] : TiApplication: ti.modules.titanium.ui.widget.TiUIScrollableView.removeView(TiUIScrollableView.java:350) [ERROR] : TiApplication: ti.modules.titanium.ui.ScrollableViewProxy.handleMessage(ScrollableViewProxy.java:131) [ERROR] : TiApplication: android.os.Handler.dispatchMessage(Handler.java:98) [ERROR] : TiApplication: android.os.Looper.loop(Looper.java:135) [ERROR] : TiApplication: android.app.ActivityThread.main(ActivityThread.java:5254) [ERROR] : TiApplication: java.lang.reflect.Method.invoke(Native Method) [ERROR] : TiApplication: java.lang.reflect.Method.invoke(Method.java:372) [ERROR] : TiApplication: com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) [ERROR] : TiApplication: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) {code} How i've solved it: Before removing the ScrollableView views, i've used this method: {code:javascript} $.scrollableView.scrollToView($.scrollableView.getViews()[0]); {code}
Story points: 5