我一直致力于按钮的实现,以更新我的页面适配器中的文本视图。这是到目前为止我为我的页面适配器编写的代码
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个文本视图更新得很好,但问题出在我的imagebutton上。当我点击按钮更新一个字符hp时,程序强制关闭,根据logcat,这是一个空指针异常,我不确定为什么会发生这种情况。
我一直在引用Button activity with viewPager?和How to write button onClick method in viewpager?,但在摆脱空指针异常方面几乎没有成功。
任何帮助都将不胜感激。谢谢!
发布于 2012-08-26 20:55:56
已经修复了你的代码。请参考以下内容:
public static class CharacterStatsPagerAdapter extends PagerAdapter {
public int getCount() {
return 2;
}
int maxhp = 0;
View view = null;
View view2 = null;
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View localView = null;
switch (position) {
case 0:
view = inflater.inflate(R.layout.page1, null);
TextView characternamedisplay = (TextView) view.findViewById(R.id.textView1);
characternamedisplay.setText(R.string.hello_world);
TextView genderdisplay = (TextView) view.findViewById(R.id.textView2);
genderdisplay.setText(R.string.hello_world);
TextView classdisplay = (TextView) view.findViewById(R.id.textView3);
classdisplay.setText(R.string.hello_world);
TextView racedisplay = (TextView) view.findViewById(R.id.textView4);
racedisplay.setText(R.string.hello_world);
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);
if (maxhpdisplay != null) {
maxhpdisplay.setText(String.valueOf(maxhp));
}
}
});
localView = view;
break;
case 1:
view2 = inflater.inflate(R.layout.page2, null);
localView = view2;
break;
}
((ViewPager) collection).addView(localView, 0);
return localView;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return (view == (View)o);
}
}问题似乎出在以下几点:您为两个视图使用了相同的变量。如果分页程序首先调用获取第一个项目,然后再调用第二个项目,那么您的变量将保存对在where中没有textView5的第二个项目的引用。
https://stackoverflow.com/questions/12128852
复制相似问题