1

I'm using a FragmentStatePagerAdapter in order to achieve a VerticalPageAdapter purpose, I have to create a dynamic number of the same Fragment regarding to which Button is pressed, So, I retrieve only the specified fragment in the getItem() method :

public Fragment getItem(int i) {            
  return new Center_ver();        
}

And the getCount() returns a dynamic number regarding to the pressed Button in other fragment:

@Override
public int getCount() {         
    return num_of_gangs;    
}

But that gave me IlligalStateException as I'm not called notifyDataSetChanged(), So, I added the notification to the method that I'm using to raising the number of Fragments:

public void add_gang() {    
    num_of_gangs ++;
    notifyDataSetChanged();     
}

but I also got the IlligalStateException :

05-04 13:43:43.210: E/AndroidRuntime(1625): FATAL EXCEPTION: main
05-04 13:43:43.210: E/AndroidRuntime(1625): java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1, found: 5 Pager id: com.automation.isolace:id/lighting_vertical_pager Pager class: class com.automation.standards.VerticalViewPager Problematic adapter: class com.automation.pageadapters.LightVerticalPageAdapter

So, I decided to add it in the getCount() method before returning the Value as follows:

@Override
public int getCount() {     
    notifyDataSetChanged();
    return num_of_gangs ;    
}

but that gave me a StackOverflowError :

05-04 13:46:36.326: E/AndroidRuntime(1690): FATAL EXCEPTION: main
05-04 13:46:36.326: E/AndroidRuntime(1690): java.lang.StackOverflowError
05-04 13:46:36.326: E/AndroidRuntime(1690):     at com.automation.standards.VerticalViewPager$PagerObserver.onChanged(VerticalViewPager.java:2717)
05-04 13:46:36.326: E/AndroidRuntime(1690):     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
05-04 13:46:36.326: E/AndroidRuntime(1690):     at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118

2 Answers2

1

You shouldn't put notifyDataSetChanged() in getCount(). notifyDataSetChanged() calls getCount(), which ends up in an infinite loop of one calling the other, known as a StackOverflow error. You should call this when you add data to the list of possible outcomes. For example, if you are adding a new Fragment, which might be stored in an ArrayList, you would do this:

public void add_gang(Fragment frag) {
    fragmentList.add(frag);
    notifyDataSetChanged();
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • well, I'm adding the same fragment Layout So I don't have to have a list of fragments, I'm always returning the same fragment and the key feature here is the number of returned fragments which is obtained by `getCount()` method, So I just add 1 to the number of the returned fragments number.so that as I wrote above I tried to call `notifyDataSetChanged()` each time an increment to the number happens but got nothing as like I didn't make any change. – Muhammed Refaat May 04 '14 at 14:22
  • +1 for informing me that `notifyDataSetChanged()` calls `getCount()` , I wasn't know that. – Muhammed Refaat May 04 '14 at 14:23
0

This basically happens when the view that populates your elements feels that it has been cheated on. Checks for that are not done on a recurrent fashion, that's why you may get the exception occasionally. It's a way for the view to tell us that there's something wrong on with the way we notify the adapter, something that we've missed or don't want obviously.

This design glitch normally appears when using references for the list used by our adapter to populate our view. This, it's always a good simplification to host a list of objects in the same activity where your adapter and view live and update it through add/addAll/remove/removeAll. Don't forget to call notifyDataSetChanged after any of those.

Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26
  • there isn't a way to add/addAll/remove/removeAll but the method I mentioned above which adds 1 to the returned number from the `getCount()` method – Muhammed Refaat May 04 '14 at 14:25
  • If you are dealing with multiple items, it'll be easier to use a list of elements to keep track of them. You can add/addAll/remove/removeAll to that list and call notifyDataSetChanged() afterwards. – Jose L Ugia May 04 '14 at 15:00
  • That way you'll also simplify your getCount() and add_gang methods. – Jose L Ugia May 04 '14 at 15:00