1

我在我的 android app 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;
    }
}
}

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

4

1 回答 1

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);
}
于 2013-08-30T08:09:13.933 回答