我有問題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);
}
}
非常感謝你的兄弟:)它的工作原理! –
請將此答案標爲正確! – JpCrow