2016-05-06 89 views
0

我有問題setOnPageChangeListener。請幫我解決這個問題。問題與setOnPageChangeListener

當我運行我的應用程序,而不setOnPageChangeListener它的作品不錯,但與此得到它墜毀,並顯示如下消息:

試圖調用虛擬方法無效android.support.v4.view.ViewPager.setOnPageChangeListener在空對象引用

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); 

    } 
} 

回答

0

在你的方法initialisePaging,你初始化viewPager你正在創造一個新的,而不是用一個聲明(android.support.v4.view.ViewPager $ OnPageChangeListener)」作爲全局(_mViewPager)。爲了解決這個問題,您需要更改以下:

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

爲:

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

非常感謝你的兄弟:)它的工作原理! –

+0

請將此答案標爲正確! – JpCrow

0

您已經定義了一個全局變量,private ViewPager _mViewPager; 但沒有任何參考分配給它。併爲ViewPager創建了一個局部變量。 ViewPager pager = (ViewPager)findViewById(R.id.vgeView);

然後,您嘗試使用全局變量來設置OnPageChangeListener。 _mViewPager.setOnPageChangeListener()

here,_mViewPager爲null。

所以,你必須改變路線, ViewPager pager = (ViewPager)findViewById(R.id.vgeView);_mViewPager = (ViewPager)findViewById(R.id.vgeView);

+0

非常感謝你的兄弟:)它的作品! –

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();} 
+0

非常感謝你的兄弟:)它的作品! –

+0

歡迎你^^ – abdoulayeYATERA