我该如何修复Android适配器中的IndexOutOfBoundsException错误

时间:2018-11-03 06:49:32

标签: java android android-viewpager android-adapter

在我的应用程序中,我想使用viewpager从服务器显示一些数据。
我在下面编写了代码,但是当在viewpager页面中的之间滑动时,显示viewpager adapter中的错误!

我的适配器代码:

public class HomeRecentPostAdapter extends EnchantedViewPagerAdapter {

    Context context;
    LayoutInflater inflater;
    ArrayList<PostsItem> model;
    View view;

    public HomeRecentPostAdapter(Context context, ArrayList<PostsItem> model) {
        super(model);
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.model = model;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        if (model.size() == 0) return null;
        view = this.inflater.inflate(R.layout.home_recent_post_row, container, false);

        ImageView homePageRecentRow_img = view.findViewById(R.id.homePageRecentRow_img);

        Glide.with(context)
                .load(model.get(position).getThumbnailImages().getFull().getUrl())
                .into(homePageRecentRow_img);

        view.setTag(EnchantedViewPager.ENCHANTED_VIEWPAGER_POSITION + position);

        container.addView(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return 5;
    }
}

我的填充适配器活动代码:

@Override
public void setRecentPostResponse(Response<RecentPostResponse> getRecentPostResponse) {
    model.clear();
    model.addAll(getRecentPostResponse.body().getPosts());
    homePage_recentViewPager.useScale();
    homePage_recentViewPager.useAlpha();
    homeRecentPostAdapter.notifyDataSetChanged();
    homeRecentPostAdapter.enableCarrousel();
    homePage_recentViewPager.setAdapter(homeRecentPostAdapter);
    homePage_recentViewPager.setCurrentItem(2);
}

强制关闭错误:

Process: example.in.videogaming, PID: 24652
java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
    at java.util.ArrayList.get(ArrayList.java:411)
    at example.in.videogaming.Pages.Home.HomeRecentPostAdapter.instantiateItem(HomeRecentPostAdapter.java:48)
    at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
    at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1224)
    at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
    at androidx.viewpager.widget.ViewPager$3.run(ViewPager.java:273)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
    at android.view.Choreographer.doCallbacks(Choreographer.java:683)
    at android.view.Choreographer.doFrame(Choreographer.java:616)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)

将此行显示到adapter时出错:

.load(model.get(position).getThumbnailImages().getFull().getUrl())

我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试一下:

@Override
public int getItemPosition(Object object) {
    return model.size();
}