我有10个片段列表。我能够刷卡并更新当前片段的值。在每个片段的顶部,我有一个数字列表,如page1,page2,page3 ... page15 所以现在我也想跳进点击的页面片段。所以我正在使用
viewPagerInstance.setCurrentItem(int position)
如果我跳到当前页面的右页左侧,它就有效。但如果我点击另一页,就像我在第2页,并想去page10。它给我空指针异常。 我的资源(按钮)出现null错误。 onCreateView是否正常工作?因为所有资源都已初始化。所以它不应该是空的。
请帮助专家
我的适配器
public class MyPageAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.scorecardtabswingfragment,
container, false);
puttsBtnList = new ArrayList<Button>();
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn0));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn1));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn2));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn3));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn4));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn5));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn6));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn7));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn8));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn9));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn10));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn11));
puttsBtnList.add((Button) mView.findViewById(R.id.puttsBtn12));
holeTitle = new TextView[18];
holeTitle[0] = (TextView) mView.findViewById(R.id.tv_hole1);
holeTitle[1] = (TextView) mView.findViewById(R.id.tv_hole2);
holeTitle[2] = (TextView) mView.findViewById(R.id.tv_hole3);
holeTitle[3] = (TextView) mView.findViewById(R.id.tv_hole4);
holeTitle[4] = (TextView) mView.findViewById(R.id.tv_hole5);
holeTitle[5] = (TextView) mView.findViewById(R.id.tv_hole6);
holeTitle[6] = (TextView) mView.findViewById(R.id.tv_hole7);
holeTitle[7] = (TextView) mView.findViewById(R.id.tv_hole8);
holeTitle[8] = (TextView) mView.findViewById(R.id.tv_hole9);
holeTitle[9] = (TextView) mView.findViewById(R.id.tv_hole10);
holeTitle[10] = (TextView) mView.findViewById(R.id.tv_hole11);
holeTitle[11] = (TextView) mView.findViewById(R.id.tv_hole12);
holeTitle[12] = (TextView) mView.findViewById(R.id.tv_hole13);
holeTitle[13] = (TextView) mView.findViewById(R.id.tv_hole14);
holeTitle[14] = (TextView) mView.findViewById(R.id.tv_hole15);
holeTitle[15] = (TextView) mView.findViewById(R.id.tv_hole16);
holeTitle[16] = (TextView) mView.findViewById(R.id.tv_hole17);
holeTitle[17] = (TextView) mView.findViewById(R.id.tv_hole18);
for(ImageButton b: firBtnList) {
b.setOnClickListener(this);
}
for(Button b: penaltyBtnList) {
b.setOnClickListener(this);
}
for(Button b : puttsBtnList) {
b.setOnClickListener(this);
}
for(Button b : storksBtnList) {
b.setOnClickListener(this);
}
for(TextView tv : holeTitle) {
tv.setOnClickListener(this);
}
gpsBtn.setOnClickListener(this);
toggleBtnPlayed.setOnClickListener(this);
//TextView tv_hole = (TextView) mView.findViewById(R.id.tv_hole);
//tv_hole.setText("Hole:"+getArguments().getInt("holeNum"));
doneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View paramView) {
if(APIUtil.isNeedForUpdate)
//update hole Golfer Scorecard.
new UpdateHoleGolferScorecard(APIUtil.holeno_Update, APIUtil.score_Update,
APIUtil.fir_Update, APIUtil.gir_Update, APIUtil.played_Update, APIUtil.putts_Update).execute();
getActivity().finish();
Intent intent = new Intent(getActivity(), ScorecardsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
swingBtn = (Button) mView.findViewById(R.id.swingBtn);
swingBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return mView;
}
更新
我正在使用可见属性的 setMenuVisibility 。我认为它首先调用onActivityCreated()。我将所有代码放入onActivityCreated()并立即工作。资源已初始化。但可以打电话
onActivityCreated first and after that setMenuVisibility. because for some reason i used it.
答案 0 :(得分:0)
终于解决了我的问题。
mViewPager.setOffscreenPageLimit(17);
我加载了所有页面。
我正在使用setMenuVisibility(),因为我不想加载上一页和下一页的更新。我只想加载当前页面。所以当页面改变时它会先调用。