この210は、それを使用する私の方法である:
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<PagerFragments> mFragments;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
this.mFragments = new ArrayList<>();
}
public void addFragment(Fragment fragment,String title) {
PagerFragments t = new PagerFragments();
t.setTitle(title);
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment) {
PagerFragments t = new PagerFragments();
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
return mFragments.get(position).getFragment();
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return mFragments.get(position).getTitle();
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return mFragments.size();
}
}
PagerFragment.java:
:
import android.support.v4.app.Fragment;
public class PagerFragments {
Fragment fragment;
String title;
public Fragment getFragment() {
return fragment;
}
public void setFragment(Fragment fragment) {
this.fragment = fragment;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}断片または活性のその後
private ViewPagerAdapter viewPagerAdapter;
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneDay), "1D");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneWeek), "1W");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneMonth), "1M");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneYear), "YTD");
viewPagerAdapter.addFragment(new RTopGainLossFragment(ytd), "1Y");
viewPagerAdapter.addFragment(new RTopGainLossFragment(max), "MAX");
viewpager.setAdapter(viewPagerAdapter);
tab.setupWithViewPager(viewpager);
[NullPointerExceptionとは何か、それを修正するにはどうすればいいですか?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -それ) – Henry