setOnPageChangeListener 的问题
issue with setOnPageChangeListener
我对 setOnPageChangeListener
有疑问。请帮我解决这个问题。
当我 运行 我的应用程序没有 setOnPageChangeListener
时它运行良好,但它会崩溃并显示此消息:
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setOnPageChangeListener(android.support.v4.view.ViewPager$OnPageChangeListener)' on a null object reference
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);
}
}
在您初始化 viewPager 的方法 initialisePaging 中,您正在创建一个新的,而不是使用声明为全局 (_mViewPager) 的那个。为了解决它,您需要更改以下内容:
ViewPager pager = (ViewPager)findViewById(R.id.vgeView);
用于:
_mViewPager = (ViewPager)findViewById(R.id.vgeView);
您已经定义了一个全局变量,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);
嗯,你的 _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();}
我对 setOnPageChangeListener
有疑问。请帮我解决这个问题。
当我 运行 我的应用程序没有 setOnPageChangeListener
时它运行良好,但它会崩溃并显示此消息:
Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setOnPageChangeListener(android.support.v4.view.ViewPager$OnPageChangeListener)' on a null object reference
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);
}
}
在您初始化 viewPager 的方法 initialisePaging 中,您正在创建一个新的,而不是使用声明为全局 (_mViewPager) 的那个。为了解决它,您需要更改以下内容:
ViewPager pager = (ViewPager)findViewById(R.id.vgeView);
用于:
_mViewPager = (ViewPager)findViewById(R.id.vgeView);
您已经定义了一个全局变量,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);
嗯,你的 _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();}