Closed neurospeech closed 8 years ago
Which version did you use? Empty ViewPager bug was fixed at https://github.com/ongakuer/CircleIndicator/issues/22 (v1.1.5 )
I am using 1.1.5
Here is how to reproduce,
after some delay (handler.postDelayed) , populate adapter and set notifyDataSetChanged()
public class MediaAdapter extends FragmentPagerAdapter {
ArrayList<MediaFragment> fragments = new ArrayList<MediaFragment>();
public MediaAdapter(FragmentManager fm) {
super(fm);
}
public void add(ImageRequest imageRequest,String videoUrl){
if(imageRequest==null && videoUrl==null)
return;
MediaFragment mediaFragment = new MediaFragment();
mediaFragment.setImageRequest(imageRequest);
mediaFragment.setVideoUrl(videoUrl);
fragments.add(mediaFragment);
this.notifyDataSetChanged();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "";
}
}
Can confirm. This is the top of the stack trace:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundResource(int)' on a null object reference
at me.relex.circleindicator.CircleIndicator.onPageSelected(CircleIndicator.java:157)
at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
This can be fixed around in the client app by calling setViewPager after changing the adapter data.
In usual samples, when you have your ViewPager already filled with items, CircleIndicator works correctly, but when Adapter has no items, (in case of loading something from web and populating later on), there is crash.
I think you need to check if position is -1 before doing anything in onPageSelected method.