这个问题困扰我有一个礼拜了、今天终于解决之、
     感谢:http://stackoverflow.com/questions/9402970/android-viewpager-throwing-indexoutofbounds-exception-when-setting-current-item
     报错信息如下:
04-11 15:01:29.092: E/AndroidRuntime(607): FATAL EXCEPTION: main
04-11 15:01:29.092: E/AndroidRuntime(607): java.lang.IndexOutOfBoundsException: index=3 count=2
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.addInArray(ViewGroup.java:2044)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.addViewInner(ViewGroup.java:1988)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.addView(ViewGroup.java:1865)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.support.v4.view.ViewPager.addView(ViewPager.java:920)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.addView(ViewGroup.java:1822)
04-11 15:01:29.092: E/AndroidRuntime(607):      at com.ynet.ilive.views.BrowsePicturesView$Adapter.instantiateItem(BrowsePicturesView.java:272)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.drawChild(ViewGroup.java:1556)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.View.draw(View.java:6743)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.View.draw(View.java:6743)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.View.draw(View.java:6743)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-11 15:01:29.092: E/AndroidRuntime(607):      at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewRoot.draw(ViewRoot.java:1407)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.os.Looper.loop(Looper.java:123)
04-11 15:01:29.092: E/AndroidRuntime(607):      at android.app.ActivityThread.main(ActivityThread.java:4627)
04-11 15:01:29.092: E/AndroidRuntime(607):      at java.lang.reflect.Method.invokeNative(Native Method)
04-11 15:01:29.092: E/AndroidRuntime(607):      at java.lang.reflect.Method.invoke(Method.java:521)
04-11 15:01:29.092: E/AndroidRuntime(607):      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-11 15:01:29.092: E/AndroidRuntime(607):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-11 15:01:29.092: E/AndroidRuntime(607):      at dalvik.system.NativeStart.main(Native Method)



PagerAdapter中instantiateItem的代码如下:
@Override
          public Object instantiateItem(View container, int position) {
               Log.i(TAG, "instantiateItem: - position: " + position);

               if (mDrawables.size() < position) {
                    return null;
               }

               // 初始化

               View view = mInflater.inflate(R.layout.browse_pics, null);

               mImg = (ImageView) view.findViewById(R.id.browse_pics_img);
               mIndicator = (TextView) view
                         .findViewById(R.id.browse_pics_indicator);
               mDigest = (TextView) view.findViewById(R.id.browse_pics_title);

               StringBuilder pageIndicator = new StringBuilder();
               pageIndicator.append(position + 1);
               pageIndicator.append("/");
               pageIndicator.append(mImgsCount);

               mIndicator.setText(pageIndicator.toString());
               if (null != mNews.getDigest() && !mNews.getDigest().equals("")) {
                    mDigest.setText(mNews.getDigest());
               } else {
                    mDigest.setText(mNews.getTitle());
               }
               mImg.setImageDrawable(mDrawables.get(position));

               Log.i(TAG, "mDrawables size: " + mDrawables.size() + ", position: "
                         + position);

               // 注意一定要填0,否则报错:IndexOutOfBoundsException
               // 详细请看:http://stackoverflow.com/questions/9402970/android-viewpager-throwing-indexoutofbounds-exception-when-setting-current-item
               ((ViewPager) container).addView(view, 0);
               //((ViewPager) container).addView(view, position); 这样会报错,错误信息如上所示

               return view;
          }



Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐