问题描述
- Android viewpage IndexOutOfBoundsException
-
y.MainActivity,ident = 1}
D/Animation( 653): hasEnded mEnded = false, this =android.view.animation.TranslateAnimation@438846f0
D/Animation( 653): hasEnded mEnded = false, this =android.view.animation.TranslateAnimation@438846f0
D/AndroidRuntime( 5019): Shutting down VM
W/dalvikvm( 5019): threadid=1: thread exiting with uncaught exception (group=0x421ba9a8)
W/dalvikvm( 5019): threadid=1: uncaught exception occurred
W/System.err( 5019): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
W/System.err( 5019): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
W/System.err( 5019): at java.util.ArrayList.get(ArrayList.java:304)
W/System.err( 5019): at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
W/System.err( 5019): at android.widget.ListView.dispatchDraw(ListView.java:3227)
W/System.err( 5019): at android.view.View.draw(View.java:14124)
W/System.err( 5019): at android.widget.AbsListView.draw(AbsListView.java:4157)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13013)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13062)
W/System.err( 5019): at android.view.View.draw(View.java:13814)
W/System.err( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
W/System.err( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13008)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13062)
W/System.err( 5019): at android.view.View.draw(View.java:13814)
W/System.err( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
W/System.err( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
W/System.err( 5019): at android.view.View.draw(View.java:14124)
W/System.err( 5019): at android.support.v4.view.ViewPager.draw(ViewPager.java:2262)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13013)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13062)
W/System.err( 5019): at android.view.View.draw(View.java:13814)
W/System.err( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
W/System.err( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13008)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13062)
W/System.err( 5019): at android.view.View.draw(View.java:13814)
W/System.err( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
W/System.err( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13008)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13062)
W/System.err( 5019): at android.view.View.draw(View.java:13814)
W/System.err( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
W/System.err( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13008)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13062)
W/System.err( 5019): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3177)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:12934)
W/System.err( 5019): at android.view.View.getDisplayList(View.java:13062)
W/System.err( 5019): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
W/System.err( 5019): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1261)
W/System.err( 5019): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5043)
W/System.err( 5019): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
W/System.err( 5019): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
W/System.err( 5019): at android.view.Choreographer.doFrame(Choreographer.java:548)
W/System.err( 5019): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
W/System.err( 5019): at android.os.Handler.handleCallback(Handler.java:800)
W/System.err( 5019): at android.os.Handler.dispatchMessage(Handler.java:100)
W/System.err( 5019): at android.os.Looper.loop(Looper.java:194)
W/System.err( 5019): at android.app.ActivityThread.main(ActivityThread.java:5469)
W/System.err( 5019): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 5019): at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err( 5019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:857)
W/System.err( 5019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
W/System.err( 5019): at dalvik.system.NativeStart.main(Native Method)
W/dalvikvm( 5019): threadid=1: calling UncaughtExceptionHandler
E/AndroidRuntime( 5019): FATAL EXCEPTION: main
E/AndroidRuntime( 5019): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
E/AndroidRuntime( 5019): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
E/AndroidRuntime( 5019): at java.util.ArrayList.get(ArrayList.java:304)
E/AndroidRuntime( 5019): at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
E/AndroidRuntime( 5019): at android.widget.ListView.dispatchDraw(ListView.java:3227)
E/AndroidRuntime( 5019): at android.view.View.draw(View.java:14124)
E/AndroidRuntime( 5019): at android.widget.AbsListView.draw(AbsListView.java:4157)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13013)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13062)
E/AndroidRuntime( 5019): at android.view.View.draw(View.java:13814)
E/AndroidRuntime( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
E/AndroidRuntime( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13008)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13062)
E/AndroidRuntime( 5019): at android.view.View.draw(View.java:13814)
E/AndroidRuntime( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
E/AndroidRuntime( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
E/AndroidRuntime( 5019): at android.view.View.draw(View.java:14124)
E/AndroidRuntime( 5019): at android.support.v4.view.ViewPager.draw(ViewPager.java:2262)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13013)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13062)
E/AndroidRuntime( 5019): at android.view.View.draw(View.java:13814)
E/AndroidRuntime( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
E/AndroidRuntime( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13008)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13062)
E/AndroidRuntime( 5019): at android.view.View.draw(View.java:13814)
E/AndroidRuntime( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
E/AndroidRuntime( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13008)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13062)
E/AndroidRuntime( 5019): at android.view.View.draw(View.java:13814)
E/AndroidRuntime( 5019): at android.view.ViewGroup.drawChild(ViewGroup.java:3195)
E/AndroidRuntime( 5019): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3045)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13008)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13062)
E/AndroidRuntime( 5019): at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3177)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:12934)
E/AndroidRuntime( 5019): at android.view.View.getDisplayList(View.java:13062)
E/AndroidRuntime( 5019): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
E/AndroidRuntime( 5019): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1261)
E/AndroidRuntime( 5019): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5043)该界面是通过:
List mFragmentList = new ArrayList();
mFragmentList.add(mOneFragment); mFragmentList.add(mTwoFragment); mFragmentList.add(mThreeFragment); mFragmentList.add(mFourFragment); mFragmentList.add(mFiveFragment); mViewPager.setSwipingDisabled(true); mViewPager.setAdapter(new MyFragmentStatePagerAdapter(getFragmentManager())); private class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter { public MyFragmentStatePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } } 高手指点下哦。 感激不尽阿
解决方案
下标超出了索引
http://blog.csdn.net/xixinyan/article/details/7451828
解决方案二:
下标超出了索引
http://blog.csdn.net/xixinyan/article/details/7451828
解决方案三:
下标超过索引,基本上可以把问题定位到list的get方法上,然而,看起来全局变量已经被add东西了,但……这种写法,问题可能性推测,下面的私有类不是继承自上面的类,或者给出的方法没有被构造方法调用,在重新new了一个对象之后,并没有对list添加元素。不好的解决办法,全局变量list前加static。好的解决办法,传list参数到私有类的构造方法中。最好的解决办法,重新分析需求进行代码重构,将adapter单独拉出来做一个类,把报错的地方改了估计就能用了……
解决方案四:
Android_ViewPage_手势滑动
android viewPage
android Viewpage之PageAdapter
解决方案五:
你的Fragment没有初始化吧?很明显add了,为何还是size=0?
解决方案六:
Invalid index 0, size is 0 看见没,你的size是0
解决方案七:
我估计你代码没贴全
List mFragmentList = new ArrayList();
首先你这里写的有问题,没有申明List 类型
应该是List mFragmentList = new ArrayList();
按照你这样写getItem里面要类型强转的
@Override
public Fragment getItem(int position) {
return (Fragment)mFragmentList.get(position);
}
同意 2楼 LiynGu 的解决办法
解决方案八:
好吧,是这个板块的问题,写的代码发出去就变了,我去
请忽略我
List< Fragment > mFragmentList = new ArrayList< Fragment >();
解决方案九:
还是错么?现在。。。 看看你的几个Fragment继承的是support.v4的fragment没?