我一直在努力实现按钮来更新我的页面适配器中的文本视图。这是迄今为止我的页面适配器的代码
public class CharacterStatsPagerAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
int maxhp = 0;
View view = null;
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (position) {
case 0:
view = inflater.inflate(R.layout.characterstats1, null);
TextView characternamedisplay = (TextView) view.findViewById(R.id.textView1);
characternamedisplay.setText(com.echaractersheet.BasicInfo.characternameresult);
TextView genderdisplay = (TextView) view.findViewById(R.id.textView2);
genderdisplay.setText(com.echaractersheet.BasicInfo.genderresult);
TextView classdisplay = (TextView) view.findViewById(R.id.textView3);
classdisplay.setText(com.echaractersheet.BasicInfo.classresult);
TextView racedisplay = (TextView) view.findViewById(R.id.textView4);
racedisplay.setText(com.echaractersheet.BasicInfo.raceresult);
ImageButton plusmaxhp = (ImageButton) view.findViewById(R.id.imageButton1);
plusmaxhp.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
maxhp = maxhp + 1;
TextView maxhpdisplay = (TextView) view.findViewById(R.id.textView5);
maxhpdisplay.setText(maxhp);
}
});
break;
case 1:
view = inflater.inflate(R.layout.characterstats2, null);
break;
}
((ViewPager) collection).addView(view, 0);
return view;
}
前 4 个文本视图更新得非常好,但问题出在我的图像按钮上。当我单击按钮更新字符 hp 时,程序强制关闭,根据 logcat,它是一个空指针异常,我不确定它为什么会发生。
我一直在用 viewPager 引用 Button 活动?以及如何在 viewpager 中编写按钮 onClick 方法?但在摆脱我的空指针异常方面收效甚微。
任何帮助将不胜感激。谢谢!