我正在使用fragmentstatepageadapter来实现viewpage。 我已经动态添加了片段但是当我在页面上滑动时,getActivity()会抛出NPE。 我也提到大多数解决方案都没有工作。
这是我的pagechange监听器
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
private int scrollState=-1;
private int oldFrag=-1;
private int newFrag=-1;
@Override
public void onPageSelected(int newPositon) {
Log.d("LMTLOGGERR", "get current page selected :: "+newPositon);
currentTabItem=newPositon;
Fragment new_fragment = mPageStateAdapter.getItem(newPositon);
new_fragment.setUserVisibleHint(true);
new_fragment.onResume();
if(oldFrag>=0){
Fragment ol_fragment= mPageStateAdapter.getItem(oldFrag);
ol_fragment.setUserVisibleHint(false);
ol_fragment.onPause();
}
newFrag=newPositon;
oldFrag=newFrag;
}
@Override
public void onPageScrolled(int position , float positionOffset, int positionOffsetPixel) {
if(positionOffset==0.0 && position+1==TabList.size() &&
scrollState==ViewPager.SCROLL_STATE_DRAGGING)
{
//viewPager.setCurrentItem(0);
}
}
@Override
public void onPageScrollStateChanged(int state) {
scrollState=state;
Log.d("LMTLOGGERR", "SCROLL_STATE_DRAGGING"+ViewPager.SCROLL_STATE_DRAGGING+"SCROLL_STATE_IDLE"+ViewPager.SCROLL_STATE_IDLE+
"SCROLL_STATE_SETTLING"+ViewPager.SCROLL_STATE_SETTLING+"current state"+state);
}
});
我的页面适配器
class PageStateAdapter extends FragmentStatePagerAdapter{
public PageStateAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int arg0) {
if(TabList.get(arg0).equalsIgnoreCase("profile")){
Fragment fragment = new VTMTMyProf();
return fragment;
}else if(TabList.get(arg0).equalsIgnoreCase("Video")){
// args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
Fragment fragment = new VTMTVideoActivity();
Bundle args = new Bundle();
Object obj = productsListData;
args.putSerializable("product",(Serializable) obj);
args.putString("check", "fragment");
fragment.setArguments(args);
return fragment;
}else if(TabList.get(arg0).equalsIgnoreCase("Document")){
Fragment fragment = new VTMTDocList();
Bundle args = new Bundle();
// args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
Object obj = productsListData;
args.putSerializable("product",(Serializable) obj);
args.putString("check", "fragment");
fragment.setArguments(args);
return fragment;
}else if(TabList.get(arg0).equalsIgnoreCase("Sales Tip")){
Fragment fragment = new VTMTSalesTip();
Bundle args = new Bundle();
// args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
Object obj = productsListData;
args.putSerializable("product",(Serializable) obj);
args.putString("check", "fragment");
fragment.setArguments(args);
return fragment;
}else if(TabList.get(arg0).equalsIgnoreCase("Quiz")){
Fragment fragment = new VTMTQuizList();
Bundle args = new Bundle();
// args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
Object obj = productsListData;
args.putSerializable("product",(Serializable) obj);
args.putString("check", "fragment");
fragment.setArguments(args);
return fragment;
}else{
//analytics
Fragment fragment = new VTMTNewAnalytics();
Bundle args = new Bundle();
// args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
Object obj = productsListData;
args.putSerializable("product",(Serializable) obj);
args.putString("check", "fragment");
fragment.setArguments(args);
return fragment;
}
我在这个片段类中获得了NPE
((Activity)mContext).runOnUiThread(new Runnable() {
public void run() {
listAdapter.notifyDataSetChanged();
}
});
我在onActtach(Activity)中初始化了mContext