我有一个 mainactivity,其中包含一个按钮单击按钮我正在显示具有五个屏幕的 viewpager。我想显示 viewpager 的操作栏屏幕更改,但是当我尝试调用
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener())
.
它向我展示了空指针异常。
这是我的代码:
MainActivity.java
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener
{
private Button mAddButtonB,mPreviousB,mNextB,mhandle,mSave;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
context = this;
mAddButtonB = (Button) findViewById(R.id.addnewB);
mAddButtonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mAddSearchV.setVisibility(View.GONE);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.customviewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
});
mViewPager.setOnPageChangeListener(new
ViewPager.SimpleOnPageChangeListener() {
@Override public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position); } });
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this)); }
}
public void onTabReselected(ActionBar.Tab tab,
android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabSelected(ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabUnselected(ActionBar.Tab tab,
android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
这是 MainActivity 中的 Adapter 类
public class SectionsPagerAdapter extends FragmentPagerAdapter {
PageZero p0;
PageOne p1;
PageTwo p2;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
/*
* p0 = new PageZero(); p1 = new PageOne(); p2 = new PageTwo();
*/
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position) {
case 0:
return new PageZero();
case 1:
return new PageOne();
case 2:
return new PageTwo();
case 3:
return new PageThree();
case 4:
return new PageFour();
}
return null;
}
/*int index = mViewPager.getCurrentItem();
SectionsPagerAdapter adapter= ((SectionsPagerAdapter)mViewPager.getAdapter()) ;
p0 =adapter.getFragment(index);*/
@Override
public int getCount() {
// Show 5 total pages.
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Red";
case 1:
return "Green";
case 2:
return "Blue";
case 3:
return "White";
case 4:
return "Orange";
}
return null;
}
}
等待任何答案。在此先感谢