ボタンクリックのボタンを含むメインアクティビティがあり、5つの画面を持つviewpagerを表示しています。ビューページャーの画面上の変更にアクションバーを表示したいのですが、
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener())
.
nullポインタ例外が表示されています。
これが私のコードです:
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;
}
}
回答を待っています。よろしくお願いします