Devlight / InfiniteCycleViewPager

Infinite cycle ViewPager with two-way orientation and interactive effect.
Apache License 2.0
5.75k stars 827 forks source link

getRealItem throws NullPointerException #61

Open mycodedstuff opened 6 years ago

mycodedstuff commented 6 years ago

I came to know about this when I tried to implement auto scroll. Even I tried to implement auto scroll myself using a separate thread but at getRealItem it throws NullPointerException.

Below is the logcat of exception :

java.lang.NullPointerException
                               at com.gigamole.infinitecycleviewpager.InfiniteCycleManager.getRealItem(InfiniteCycleManager.java:370)
                               at com.gigamole.infinitecycleviewpager.HorizontalInfiniteCycleViewPager.getRealItem(HorizontalInfiniteCycleViewPager.java:226)
                               at in.sabstore.HomeActivity$5$1.run(HomeActivity.java:409)
srfaytkn commented 6 years ago

+1

hongwei-bai commented 6 years ago

I had exactly the same question. This is a minor bug of this library. But you can avoid this simply by adding at least 3 items to PagerAdapter before you set it to HorizontalInfiniteCycleViewPager.

The reason why it crashes and throws a null pointer exception is, in class com.gigamole.infinitecycleviewpager.InfiniteCycleManager Line 313

public PagerAdapter setAdapter(final PagerAdapter adapter) { // If adapter count bigger then 2 need to set InfiniteCyclePagerAdapter if (adapter != null && adapter.getCount() >= MIN_CYCLE_COUNT) { mItemCount = adapter.getCount(); mInfiniteCyclePagerAdapter = new InfiniteCyclePagerAdapter(adapter); mInfiniteCyclePagerAdapter.setOnNotifyDataSetChangedListener(this); return mInfiniteCyclePagerAdapter; } else { if (mInfiniteCyclePagerAdapter != null) { mInfiniteCyclePagerAdapter.setOnNotifyDataSetChangedListener(null); mInfiniteCyclePagerAdapter = null; } return adapter; } }

If you do not have 3(MIN_CYCLE_COUNT) or more items in adapter, the member mInfiniteCyclePagerAdapter will not be created. So when you invoke getRealItem() it crashes.