我一直致力于实现按钮,以便在我的pageadapter中更新textview。这是我到目前为止我的pageadapter的代码
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个textviews更新完美,但问题出现在我的图像按钮上。当我单击按钮更新字符hp时,程序强制关闭并根据logcat,它是一个空指针异常,我不确定它为什么会发生。
我一直在引用Button activity with viewPager?和How to write button onClick method in viewpager?但是在摆脱空指针异常方面几乎没有成功。
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
修复了你的代码。 请参考以下内容:
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);
}
}
似乎问题如下:您已为两个视图使用了相同的变量。如果寻呼机首先调用以获取第一个项目,然后再调用第二个项目,那么您的变量将保留对第二个项目的引用,该项目中没有textView5。