程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-不能在view paper中設置text的問題

android-不能在view paper中設置text的問題

編輯:編程綜合問答
不能在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;

    }

最佳回答:


使用:

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

然後按照下面的方法找到文本視圖:

score = (TextView) view.findViewById(R.id.textView1);
score.setText("TEST");
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved