setOnPageChangeListener的问题

时间:2016-05-06 13:04:13

标签: android android-pageradapter

我对setOnPageChangeListener有疑问。请帮我解决这个问题。

当我在没有setOnPageChangeListener的情况下运行我的应用程序时,它运行良好,但是这样它会崩溃并显示此消息:

  

尝试在空对象引用上调用虚方法'void android.support.v4.view.ViewPager.setOnPageChangeListener(android.support.v4.view.ViewPager $ OnPageChangeListener)'

public class HomeActivity extends FragmentActivity {

    private PagerAdapter mPagerAdapter;
    private ViewPager _mViewPager;
    private ImageView _btn1, _btn2, _btn3;
    private Dialog view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ReplaceFont.replaceDefaultFont(this, "DEFAULT", "arial.ttf");
        initialisePaging();
        onCircleButtonClick();
        setTab();

    }


    private void initialisePaging(){

        List<Fragment> fragments= new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, ImageOne.class.getName()));
        fragments.add(Fragment.instantiate(this, ImageTwo.class.getName()));
        fragments.add(Fragment.instantiate(this, ImageThree.class.getName()));

        mPagerAdapter = new ImagePagerAdapter(this.getSupportFragmentManager(),fragments);
        ViewPager pager = (ViewPager)findViewById(R.id.vgeView);
        pager.setAdapter(mPagerAdapter);
        initButton();
    }
    private void onCircleButtonClick() {

        _btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _btn1.setImageResource(R.drawable.fill_circle);
                _mViewPager.setCurrentItem(0);
            }
        });

        _btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _btn2.setImageResource(R.drawable.fill_circle);
                _mViewPager.setCurrentItem(1);
            }
        });
        _btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _btn3.setImageResource(R.drawable.fill_circle);
                _mViewPager.setCurrentItem(2);
            }
        });
    }
    private void setTab() {
        _mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int position) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int position) {

                _btn1.setImageResource(R.drawable.holo_circle);
                _btn2.setImageResource(R.drawable.holo_circle);
                _btn3.setImageResource(R.drawable.holo_circle);
                btnAction(position);
            }

        });

    }
    private void btnAction(int action) {
        switch (action) {
            case 0:
                _btn1.setImageResource(R.drawable.fill_circle);

                break;

            case 1:
                _btn2.setImageResource(R.drawable.fill_circle);

                break;
            case 2:
                _btn3.setImageResource(R.drawable.fill_circle);

                break;
        }
    }

    private void initButton() {
        _btn1 = (ImageView)findViewById(R.id.btn1);
        _btn1.setImageResource(R.drawable.fill_circle);
        _btn2 = (ImageView)findViewById(R.id.btn2);
        _btn3 = (ImageView)findViewById(R.id.btn3);

    }
}

3 个答案:

答案 0 :(得分:0)

在初始化viewPager的方法initialisePaging中,您正在创建一个新的,而不是使用声明为global(_mViewPager)的方法。为了解决这个问题,您需要更改以下内容:

 ViewPager pager = (ViewPager)findViewById(R.id.vgeView);

for:

 _mViewPager = (ViewPager)findViewById(R.id.vgeView);

答案 1 :(得分:0)

您已定义全局变量private ViewPager _mViewPager; 但没有指定任何参考。并为ViewPager创建了一个局部变量。 ViewPager pager = (ViewPager)findViewById(R.id.vgeView);

然后,您尝试使用全局变量来设置OnPageChangeListener。 _mViewPager.setOnPageChangeListener()

这里,_mViewPager为空。

所以,你必须改变线, ViewPager pager = (ViewPager)findViewById(R.id.vgeView);_mViewPager = (ViewPager)findViewById(R.id.vgeView);

答案 2 :(得分:0)

嗯你的_mViewPager没有实例化。

替换

private void initialisePaging(){

List<Fragment> fragments= new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, ImageOne.class.getName()));
fragments.add(Fragment.instantiate(this, ImageTwo.class.getName()));
fragments.add(Fragment.instantiate(this, ImageThree.class.getName()));

mPagerAdapter = new ImagePagerAdapter(this.getSupportFragmentManager(),fragments);
ViewPager pager = (ViewPager)findViewById(R.id.vgeView);
pager.setAdapter(mPagerAdapter);
initButton();}

用这个

enter code private void initialisePaging(){

List<Fragment> fragments= new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, ImageOne.class.getName()));
fragments.add(Fragment.instantiate(this, ImageTwo.class.getName()));
fragments.add(Fragment.instantiate(this, ImageThree.class.getName()));

mPagerAdapter = new ImagePagerAdapter(this.getSupportFragmentManager(),fragments);
_mViewPager = (ViewPager)findViewById(R.id.vgeView);
_mViewPager.setAdapter(mPagerAdapter);
initButton();}