1

所以这里是代码:调用 SectionsPageradapter 构造函数:

mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

SectionsPagerAdapter 类:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @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.
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

和 TabFragment 类:

/**
 * A fragment that launches other parts of the demo application.
 */
public static class TabFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);

        int tabLayout = 0;
        switch (position) {
        case 0:
            tabLayout = R.layout.activity_file_list;
            break;
        case 1:
            tabLayout = R.layout.panel_view;
            break;

        }

        View rootView = inflater.inflate(tabLayout, container, false);

        return rootView;
    }
}

所以错误是当我调用 mViewPager.setAdapter(mSectionsPagerAdapter); 它说 mSectionsPagerAdapter 为空。

有任何想法吗 ?

谢谢,尼克

4

0 回答 0