0

我一直在努力实现按钮来更新我的页面适配器中的文本视图。这是迄今为止我的页面适配器的代码

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 方法?但在摆脱我的空指针异常方面收效甚微。

任何帮助将不胜感激。谢谢!

4

1 回答 1

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。

于 2012-08-26T12:55:55.787 回答