我正在使用 aFragmentStatePagerAdapter
为了达到一个VerticalPageAdapter
目的,我必须创建一个动态编号的相同 Fragment 关于哪个Button
被按下,所以,我只检索getItem()
方法中的指定片段:
public Fragment getItem(int i) {
return new Center_ver();
}
并getCount()
返回一个关于Button
在其他片段中按下的动态数字:
@Override
public int getCount() {
return num_of_gangs;
}
但这给了我IlligalStateException
,因为我没有被调用notifyDataSetChanged()
,所以,我将通知添加到我用来提高片段数量的方法中:
public void add_gang() {
num_of_gangs ++;
notifyDataSetChanged();
}
但我也得到了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
因此,我决定在返回 Value 之前将其添加到 getCount() 方法中,如下所示:
@Override
public int getCount() {
notifyDataSetChanged();
return num_of_gangs ;
}
但这给了我一个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)