Android片段不会显示任何内容

时间:2013-08-30 08:01:41

标签: android fragment

我有我的Android应用程序2片段,但不会显示。这是我的代码:

 private static final int NUMBER_OF_PAGES = 2;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_canzoni);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager)findViewById(R.id.pager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.canzoni, menu);
    return true;
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {
    private Fragment[] fragments = new Fragment[] { new Song(), new Bans()};
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment=new Fragment();
        // 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 fragment =new Song();
            case 1:
                return fragment = new Bans();
            default:
                break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 2;
    }

@Override
public CharSequence getPageTitle(int position){
    Locale l= Locale.getDefault();
    String lol="Canzoni";
    String asd="Bans";
    switch(position){
        case 0:
            return getString(R.string.title_activity_canzoni).toUpperCase(l);
        case 1:
            return getString(R.string.title_activity_bans).toUpperCase(l);
    }
    return null;
}

}
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}
}

任何解决方案或想法?屏幕只显示没有标题或任何内容的viewpager ...我的片段是看不见的!

1 个答案:

答案 0 :(得分:2)

你缺少mViewPager.setAdapter(mSectionsPagerAdapter);

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_canzoni);
  mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  mViewPager = (ViewPager)findViewById(R.id.pager);
  mViewPager.setAdapter(mSectionsPagerAdapter);
}