1

I have currently implemented a viewpager that flips through three different layouts smoothly - all working well. The problem comes when I try and access elements within them layouts, I dont know where to begin? Can viewflipper even do this? I assume so because it is being done in the marketpalce etc?

Activity:

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            //pagerviwer

            MyPagerAdapter adapter = new MyPagerAdapter();
            ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(1); 

PageAdapter:

class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    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.training_topics;
            break;
        case 1:
            resId = R.layout.behaviour_topics;

            break;
        case 2:
            resId = R.layout.tricks_topics;
            break;
        }

        View v = inflater.inflate(resId, null);

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

        return v;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

Example: Say for example if I was trying to access an ImageView inside traning_topics.xml? Doing this through the main activity just crashes it.

Jonno
  • 1,542
  • 3
  • 22
  • 39

1 Answers1

4

I'm experiencing this as well. It's trying to access a null pointer when you refer to your layout, thus you get an exception. Here is the solution I found:

Android ViewPager findViewById not working - Always returning null

It appears that you must first inflate the view you want inside the instantiateItem(View collection, int position) method. Then, you must initialize it by calling findViewById.

You should do this for both the root node of your layout and the specific widget you are trying to access. I will try to verify this solution, but is seems to make sense. The only foreseeable problem is if you want to manipulate the view widget outside this method.

ViewPager is relatively new, and I think that the Android Dev Team will be changing/improving some if its features in the future. I hope this helps, let me know.

Community
  • 1
  • 1
  • This is exactly right! Worded perfectly, I will try this code out later and let you know! Thanks! – Jonno Jan 29 '12 at 01:30