首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在页面适配器中实现按钮

在页面适配器中实现按钮
EN

Stack Overflow用户
提问于 2012-08-26 16:58:57
回答 1查看 1.6K关注 0票数 0

我一直致力于按钮的实现,以更新我的页面适配器中的文本视图。这是到目前为止我为我的页面适配器编写的代码

代码语言:javascript
复制
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?,但在摆脱空指针异常方面几乎没有成功。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-26 20:55:56

已经修复了你的代码。请参考以下内容:

代码语言:javascript
复制
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的第二个项目的引用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12128852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档