The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDat

如果在logcat日志中出现以下错误:  


01-22 09:24:42.271: E/AndroidRuntime(11279): java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 1 Pager id: com.agilespeech.heyyou:id/select_element_viewPager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.agilespeech.heyyou.activity.SelectElement$3



解决方法:  



在初始化ViewPager时,应先给Adapter初始化内容后再将该adapter传给ViewPager,如果不这样处理, 会报这个错。 在更新 

adapter的内容后,应该调用一下adapter的notifyDataSetChanged方法。意思就是viewpager 里面adapter view要有,哪怕为空也行。

private List<View> viewList = new ArrayList<View>();

Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 4:
pagerAdapter.notifyDataSetChanged();
progressbar.setVisibility(View.GONE);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_element);
initView();
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
viewList.clear();
for (int i = 0; i < MyApplication.getInstance().themeList
.size(); i++) {
View v = LayoutInflater.from(SelectElement.this).inflate(
R.layout.select_element_viewpager, null);
gridview = (GridView) v
.findViewById(R.id.select_element_gridView);
ElementAdapter adapter = new ElementAdapter(
SelectElement.this, i);
gridview.setAdapter(adapter);
viewList.add(v);
adapterList.add(adapter);
selectAll.add(false);
for (Element element : MyApplication.getInstance().themeList
.get(i).element)
MyApplication.getInstance().loadSound(element);
}
currentAdapter = adapterList.get(0);
handler.sendEmptyMessage(4);
Looper.loop();
}
}).start();
}
private void initView() {

viewPager = (ViewPager) findViewById(R.id.select_element_viewPager);
viewList.add(null);//问题在这里,开始没有这句话
pagerAdapter = new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView(viewList.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position), 0);
return viewList.get(position);
}
};
viewPager.setAdapter(pagerAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
currentAdapter = adapterList.get(arg0);
checkBox.setChecked(selectAll.get(arg0));
position = arg0;
title.setText("主题名称:"
+ MyApplication.getInstance().themeList.get(position).themeName);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}


看上面的代码,因为初始化数据很耗时间,我在新线程里面初始化然后handler 更新,但是刚开始没有那句红色的话,虽然有

private List<View> viewList = new ArrayList<View>();但是viewlist里面没有东西在viewpager刚开始的时候就会报错。

add一个null,说明有一个界面了,所以不会报错。意思很明显:

在初始化ViewPager时,应先给Adapter初始化内容后再将该adapter传给ViewPager,如果不这样处理,会报这个错。

后面你在初始化重新刷新布局想要的数据又会出现了,这里做的就是这个原理。


你可能感兴趣的:(android)