如何从viewpager引用activity元素

时间:2012-12-04 16:10:57

标签: android android-viewpager

我尝试编写一个应用程序,即使用幻灯片效果进行活动,发现兼容包中的ViewPager是我需要的。我成功制作了幻灯片,它以我想要的方式工作,但我的问题是我无法引用我的活动中的元素。

这是我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity.java:

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

        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.pager);

        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);

        if (Fibonacci.getInstance().number != 0)
            displayResults();

        ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);
        btnCalculate.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // do button click
            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {
        public int getCount() {
            return 2;
        }

        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.activity_dark;
                break;
            case 1:
                resId = R.layout.activity_pale;
                break;
            }
            View view = inflater.inflate(resId, null);
            ((ViewPager) collection).addView(view, 0);
            return view;
        }

        @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;
        }
    }
}

我的问题是我收到以下行的NullPointerEception:

ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);

我知道这是因为在setContentView中我设置了main.xml,我想引用activity_dark.xml或activity_pale.xml中的元素。不幸的是,我无法弄清楚如何引用该元素。任何提示将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

你最好使用Fragments http://developer.android.com/guide/components/fragments.html

但是如果你想这样做,那么尝试在页面改变后设置监听器:

 myPager.setOnPageChangeListener(new OnPageChangeListener(){

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageSelected(int arg0) {
                ImageButton btnCalculate = (ImageButton) findViewById(R.id.btnCalculateDark);
                btnCalculate.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        // do button click
                    }
                });
            }

        });