我一直在拉我的頭髮一整天 - 我從PagerAdapter繼承,並從我的主要活動調用以下內容。 問題是,下面的測試總是返回null
。 另外,當我調試時,我發現MyPagerAdapter.java中的instantiateItem方法根本沒有輸入!是什麼賦予了?Android - PagerAdapter實例化對象不被調用
感謝任何幫助,我可以得到。 謝謝。
碼頭。
// For the paging swipes
MyPagerAdapter adapter = new MyPagerAdapter();
m_ViewPager = (ViewPager) findViewById(R.id.myfivepanelpager);
adapter.notifyDataSetChanged();
m_ViewPager.setAdapter(adapter);
m_ViewPager.setCurrentItem(2);
View test = m_ViewPager.getChildAt(2);
ListView theList = (ListView) test.findViewById(R.id.listView1);
MyPagerAdapter.java
public class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//Log.d("Instantiate Item", null);
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View view = null;
View view;
int resId = 0;
switch (position) {
case 0:
resId = R.layout.page1;
view = inflater.inflate(resId, null);
TextView txtView =(TextView)view.findViewById(R.id.textView1);
ListView lstView = (ListView)view.findViewById(R.id.listView1);
break;
case 1:
resId = R.layout.page2;
view = inflater.inflate(resId, null);
TextView txtView2 =(TextView)view.findViewById(R.id.textView2);
break;
case 2:
resId = R.layout.page3;
view = inflater.inflate(resId, null);
TextView txtView3 =(TextView)view.findViewById(R.id.textView3);
break;
case 3:
resId = R.layout.page4;
view = inflater.inflate(resId, null);
TextView txtView4 =(TextView)view.findViewById(R.id.textView4);
break;
case 4:
resId = R.layout.page5;
view = inflater.inflate(resId, null);
TextView txtView5 =(TextView)view.findViewById(R.id.textView5);
break;
default:
resId = R.layout.page1;
view = inflater.inflate(resId, null);
break;
}
((ViewPager) container).addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
通過測試失敗你的意思是你的theList是空的? – sandrstar