ViewPager并更新TextView;致命的例外?

时间:2014-01-22 10:57:50

标签: android exception android-viewpager

我正在尝试让ViewPager工作,但我的代码的一部分导致了问题。有问题的代码部分直接发布在下面。从我怀疑,我的TextViews被重复更新,因为位置被迭代到方法getCount()中设置的值,这是我的数组的大小。

无论如何,下面所有这些都是我的代码,因为我到目前为止。

来自ViewPagerAdapter.java的有问题的代码

            // Capture position and set to the TextViews
            tvNumber.setText(arrayAtomicNum.get(position));
            tvSymbol.setText(arraySymbol.get(position));
            tvName.setText(arrayName.get(position));
            tvGroup.setText(arrayGroup.get(position));
            tvPeriod.setText(arrayPeriod.get(position));
            tvBlock.setText(arrayBlock.get(position));
            tvFamily.setText(arrayFamily.get(position));
            tvColor.setText(arrayColor.get(position));
            tvPhase.setText(arrayPhase.get(position));
            tvNeutrons.setText(arrayNeutrons.get(position));
            tvProtons.setText(arrayProtons.get(position));
            tvElectrons.setText(arrayElectrons.get(position));

这是全班ViewPagerAdapter.java

public class ViewPagerAdapter extends PagerAdapter {
    public static String TAG = ViewPagerAdapter.class.getSimpleName();

    Context context;
    LayoutInflater inflater;


    List<Integer> arrayAtomicNum = new ArrayList<Integer>();
    List<String> arrayName = new ArrayList<String>();
    List<String> arraySymbol = new ArrayList<String>();
    List<String> arrayFamily = new ArrayList<String>();
    List<String> arrayPhase = new ArrayList<String>();
    List<String> arrayColor = new ArrayList<String>();
    List<Integer> arrayGroup = new ArrayList<Integer>();
    List<Integer> arrayPeriod = new ArrayList<Integer>();
    List<String> arrayBlock = new ArrayList<String>();
    List<Integer> arrayProtons = new ArrayList<Integer>();
    List<Integer> arrayNeutrons = new ArrayList<Integer>();
    List<Integer> arrayElectrons = new ArrayList<Integer>();    


    public ViewPagerAdapter(Context context, List<Integer> arrayAtomicNum, 
            List<String> arrayName, List<String> arraySymbol, List<String> arrayFamily,
            List<String> arrayPhase, List<String> arrayColor, List<Integer> arrayGroup, 
            List<Integer> arrayPeriod, List<String> arrayBlock, List<Integer> arrayProtons,
            List<Integer> arrayNeutrons, List<Integer> arrayElectrons) {
        this.context = context;
        this.arrayAtomicNum = arrayAtomicNum;
        this.arrayName = arrayName;
        this.arraySymbol = arraySymbol;
        this.arrayFamily = arrayFamily;
        this.arrayPhase = arrayPhase;
        this.arrayColor = arrayColor;
        this.arrayGroup = arrayGroup;
        this.arrayPeriod = arrayPeriod;
        this.arrayBlock = arrayBlock;
        this.arrayProtons = arrayProtons;
        this.arrayNeutrons = arrayNeutrons;
        this.arrayElectrons = arrayElectrons;

    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        //return 0;
        return arrayAtomicNum.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        // TODO Auto-generated method stub
        //return false;
        return view == ((RelativeLayout) object);

    }

     @Override
        public Object instantiateItem(ViewGroup container, int position) {

            // Declare Variables
            TextView tvNumber; // container for atomic number
            TextView tvSymbol; // container for symbol
            TextView tvName; // container for name
            TextView tvGroup; // container for group
            TextView tvPeriod; // container for period
            TextView tvBlock; // container for block
            TextView tvFamily; // container for family
            TextView tvColor; // container for color
            TextView tvPhase; // container for phase
            TextView tvNeutrons; // container for neutrons
            TextView tvProtons; // container for protons
            TextView tvElectrons; // container for electrons

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View itemView = inflater.inflate(R.layout.frame_learn, container,
                    false);

            // Locate the TextViews in viewpager_item.xml
            // atomic number textView
            tvNumber = (TextView)itemView.findViewById(R.id.metanumber);

            // symbol textView
            tvSymbol = (TextView)itemView.findViewById(R.id.metasymbol);

            // name textView
            tvName = (TextView)itemView.findViewById(R.id.metaname);

            // group textView
            tvGroup = (TextView)itemView.findViewById(R.id.metagroup);

            // period textView
            tvPeriod = (TextView)itemView.findViewById(R.id.metaperiod);

            // block textView
            tvBlock = (TextView)itemView.findViewById(R.id.metablock);

            // family textView
            tvFamily = (TextView)itemView.findViewById(R.id.metafamily);

            // color textView
            tvColor = (TextView)itemView.findViewById(R.id.metacolor);

            // phase textView
            tvPhase = (TextView)itemView.findViewById(R.id.metaphase);

            // neutrons textView
            tvNeutrons = (TextView)itemView.findViewById(R.id.metaneutrons);

            // protons textView
            tvProtons = (TextView)itemView.findViewById(R.id.metaprotons);

            // electrons textView
            tvElectrons = (TextView)itemView.findViewById(R.id.metaelectrons);

            //comment out the setText and logs will print out full arrayLists
            Log.d(TAG, "arrayAtomicNum.get(position): "  + arrayAtomicNum.get(position));
            Log.d(TAG, "arraySymbol.get(position): "  + arraySymbol.get(position));
            Log.d(TAG, "arrayName.get(position): "  + arrayName.get(position));

            // Capture position and set to the TextViews
            tvNumber.setText(arrayAtomicNum.get(position));
            tvSymbol.setText(arraySymbol.get(position));
            tvName.setText(arrayName.get(position));
            tvGroup.setText(arrayGroup.get(position));
            tvPeriod.setText(arrayPeriod.get(position));
            tvBlock.setText(arrayBlock.get(position));
            tvFamily.setText(arrayFamily.get(position));
            tvColor.setText(arrayColor.get(position));
            tvPhase.setText(arrayPhase.get(position));
            tvNeutrons.setText(arrayNeutrons.get(position));
            tvProtons.setText(arrayProtons.get(position));
            tvElectrons.setText(arrayElectrons.get(position));

            // Add viewpager_item.xml to ViewPager
            ((ViewPager) container).addView(itemView);

            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // Remove viewpager_item.xml from ViewPager
            ((ViewPager) container).removeView((RelativeLayout) object);

        }
}

我在另一个名为LearnActivity.java的Activity中填充我的arrayLists

public class LearnActivity extends Activity {
    public static String TAG = LearnActivity.class.getSimpleName();

    ViewPager viewPager;
    PagerAdapter adapter;

    private int atomicNum = 0;

    private int temp1;
    private int temp2;

    List<Integer> arrayAtomicNum = new ArrayList<Integer>();
    List<String> arrayName = new ArrayList<String>();
    List<String> arraySymbol = new ArrayList<String>();
    List<String> arrayFamily = new ArrayList<String>();
    List<String> arrayPhase = new ArrayList<String>();
    List<String> arrayColor = new ArrayList<String>();
    List<Integer> arrayGroup = new ArrayList<Integer>();
    List<Integer> arrayPeriod = new ArrayList<Integer>();
    List<String> arrayBlock = new ArrayList<String>();
    List<Integer> arrayProtons = new ArrayList<Integer>();
    List<Integer> arrayNeutrons = new ArrayList<Integer>();
    List<Integer> arrayElectrons = new ArrayList<Integer>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_learn);
        Log.d(TAG, "Learn layout loaded successfully");

        while (atomicNum < 3) {
        atomicNum++;

        if (atomicNum == 1) {
            ElementStructure.setName("Hydrogen");
            ElementStructure.setSymbol("H");
            ElementStructure.setFamily("Non metals");
            ElementStructure.setPhase("Gas");
            ElementStructure.setColor("Colorless");
            ElementStructure.setAtomicNum(atomicNum);
            ElementStructure.setGroup(1);
            ElementStructure.setPeriod(1);
            ElementStructure.setBlock("s");
            ElementStructure.setProton(atomicNum);
        } else if (atomicNum == 2) {
            ElementStructure.setName("Helium");
            ElementStructure.setSymbol("He");
            ElementStructure.setFamily("Noble gases");
            ElementStructure.setPhase("Gas");
            ElementStructure.setColor("Colorless");
            ElementStructure.setAtomicNum(atomicNum);
            ElementStructure.setGroup(18);
            ElementStructure.setPeriod(1);
            ElementStructure.setBlock("s");
            ElementStructure.setProton(atomicNum);
        } else if (atomicNum == 3) {
            ElementStructure.setName("Lithium");
            ElementStructure.setSymbol("Li");
            ElementStructure.setFamily("Alkali metals");
            ElementStructure.setPhase("Solid");
            ElementStructure.setColor("Silvery white");
            ElementStructure.setAtomicNum(atomicNum);
            ElementStructure.setGroup(1);
            ElementStructure.setPeriod(2);
            ElementStructure.setBlock("s");
        }

        // Atoms must have equal number(s) of protons and electrons 
        ElementStructure.setElectron(ElementStructure.getProton());

        /* Legend:
         * temp1-atomic weight rounded to nearest whole integer
         * temp2-number of neutrons */
        //temp1 = (int)Math.round(ElementStructure.getWeight());
        temp1 = 100; //just a test value
        temp2 = temp1 - ElementStructure.getProton();
        ElementStructure.setNeutron(temp2);

        // Populate arrays
        arrayAtomicNum.add(ElementStructure.getAtomicNum());
        arrayName.add(ElementStructure.getName());
        arraySymbol.add(ElementStructure.getSymbol());
        arrayFamily.add(ElementStructure.getFamily());
        arrayPhase.add(ElementStructure.getPhase());
        arrayColor.add(ElementStructure.getColor());
        arrayGroup.add(ElementStructure.getGroup());
        arrayPeriod.add(ElementStructure.getPeriod());
        arrayBlock.add(ElementStructure.getBlock());
        arrayProtons.add(ElementStructure.getProton());
        arrayNeutrons.add(ElementStructure.getNeutron());
        arrayElectrons.add(ElementStructure.getElectron());

        } //end while loop


        viewPager = (ViewPager)findViewById(R.id.pager);
        viewPager.setOffscreenPageLimit(3);
        adapter = new ViewPagerAdapter(LearnActivity.this, arrayAtomicNum, arrayName,
                arraySymbol, arrayFamily, arrayPhase, arrayColor, arrayGroup, arrayPeriod,
                arrayBlock, arrayProtons, arrayNeutrons, arrayElectrons);
        viewPager.setAdapter(adapter);


    }

}

所有数组列表都有正确的值,似乎我已经将我想要的所有数据正确关联起来。还有更多,但我试图保持这个帖子有限。在任一情况下, 您认为导致以下错误的是什么?我知道在尝试setText时会发生这种情况,但为什么呢?

1 个答案:

答案 0 :(得分:2)

如果要将整数作为文本设置为TextView,则应使用

textView.setText(String.valueOf(intValue));

如果只设置int值,TextView将尝试使用该int值访问资源。