2 clcurie 11 clcurie_11 于 2012.10.19 15:56 提问

不能在view paper中设置text的问题

我想在viewpager的图像上添加一个textview,也就相当于text视图附加在paper视图上。当我使用setText()方法时,系统会提醒score.setText("TEST")行返回一个空指针异常。这是相关代码:

private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 5;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);         

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.farleft;

            score = (TextView) findViewById(R.id.textView1);
            **EXCEPTION HERE**      score.setText("TEST");

            break;
        case 1:
            resId = R.layout.left;
            break;
        case 2:
            resId = R.layout.middle;
            break;
        case 3:
            resId = R.layout.right;
            break;
        case 4:
            resId = R.layout.farright;
            break;
        }

        View view = inflater.inflate(resId, null);
        view.setTag(position);

        view.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {

                int position = (Integer) view.getTag();
                Log.v("Log_tag", "Here image is clicked" + position);

                if(position == 0){
                    Intent intent = new Intent(LogoQuizActivity.this,
                            LevelOne.class);
                    startActivity(intent);

                }

                if(position == 1){
                    Intent intent = new Intent(getBaseContext(),
                            LevelTwo.class);
                    startActivity(intent);
                }

                if(position == 2){
                    Intent intent = new Intent(getBaseContext(),
                            LevelThree.class);
                    startActivity(intent);
                }

                if(position == 3){
                    Intent intent = new Intent(getBaseContext(),
                            LevelFour.class);
                    startActivity(intent);
                }

                if(position == 4){
                    Intent intent = new Intent(getBaseContext(),
                            LevelFive.class);
                    startActivity(intent);
                }
            }
        });


        ((ViewPager) collection).addView(view, 0);

        return view;

    }

2个回答

Baby_Bonnie
Baby_Bonnie   2012.10.29 11:41
已采纳

使用:

View view = (View) inflater.inflate(R.layout.your_layout, null);

然后按照下面的方法找到文本视图:

score = (TextView) view.findViewById(R.id.textView1);
score.setText("TEST");
yongyong_21
yongyong_21   2012.10.19 17:23

你没在视图findViewById(R.id.textView1)前设置布局内容

...
    View view = inflater.inflate(resId, null); 
    switch (position) {
    case 0:
        resId = R.layout.farleft;

        score = (TextView) view.findViewById(R.id.textView1);
        score.setText("TEST");

        break;
    case 1:
        resId = R.layout.left;
        break;
    case 2:
        resId = R.layout.middle;
        break;
    case 3:
        resId = R.layout.right;
        break;
    case 4:
        resId = R.layout.farright;
        break;
    }
...
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!