每当我尝试viewPager.getChildAt时,我都会收到空指针异常 我附上了代码snippshot
// Scroll Page Limit
private int scrollPageLimit = Integer.MAX_VALUE / 2;
private int lastPageSelected = scrollPageLimit;
viewPager = (ViewPager) findViewById(R.id.calendar);
MyPagerAdapter adapter = new MyPagerAdapter(context);
//PagerAdapter wrappedAdapter = new InfinitePagerAdapter(adapter);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(scrollPageLimit);
LinearLayout view = (LinearLayout) viewPager.getChildAt(viewPager.getCurrentItem());
GridView calendar = (GridView) view.findViewById(R.id.gridView_calendar);
// My Adapter
private class MyPagerAdapter extends PagerAdapter {
// int NumberOfPages = scrollPageLimit*2 + 1;
Context context;
public MyPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.v("ExtendedCalendarView", "instantiateItem : "+position );
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout) inflater.inflate(
R.layout.viewpager_calendar, null);
GridView calendar = (GridView) view
.findViewById(R.id.gridView_calendar);
<!-- Calendar generation code -->
view.setTag(new MonthYearTagPager(cal.get(Calendar.MONTH), cal.get(Calendar.YEAR)));
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Log.v("ExtendedCalendarView", "destroyItem : "+position );
container.removeView((LinearLayout) object);
}
}
<!--layout viewpager_calendar -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F0EB"
android:orientation="vertical" >
<TextView
android:id="@+id/textView_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceSmallInverse" />
<GridView
android:id="@+id/gridView_calendar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:listSelector="@android:color/transparent"
android:numColumns="7">
</GridView>
如果我将访问LinearLayout view =(LinearLayout)viewPager.getFocusedChild();然后它工作,但它返回下一个viewpager元素。