FrameLayout在ViewPager

时间:2016-01-22 09:57:42

标签: java android android-fragments android-viewpager

我想在ViewPager的片段中显示与NavigationView(SlideMenu)相关的片段。

就像那样:

ViewPager:

  • MainFragment:
    • 主页
    • ...
    • 设置
  • 列出聊天
  • Tchat

现在我的MainFragment收到了错误: android.support.v4.app.FragmentActivity.getSupportFragmentManager() on a null object reference

public class MainFragment extends Fragment {
    private float lastX, currentX;
    private static int lastView = R.id.accueil;
    private FrameLayout frameLayout = null;
    private LinearLayout linearLayout;
    private FragmentManager fragmentManager;

    public MainFragment(FragmentManager fragmentManager){
        this.fragmentManager = fragmentManager;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = (View) inflater.inflate(R.layout.main_fragment, container, false);

        frameLayout = (FrameLayout) rootView.findViewById(R.id.frame_container);

        if (savedInstanceState == null)
            displayView(R.id.accueil);
        else if (frameLayout != null)
            displayView(lastView);

        return rootView;
    }

    public Fragment displayView(int id){
        Fragment fragment = null;
        lastView = id;
        switch (id){
            case 0:
                fragment = new Profil();
                break;
            case R.id.accueil:
                fragment = new MoyenTransport();
                break;
            case R.id.travel:
                fragment = new MyTravel();
                break;
            case R.id.ayruuTravel:
                fragment = new AyruuTravel();
                break;
            case R.id.carteEmbarquement:
                fragment = new CarteEmbarquement();
                break;
            case R.id.community:
                fragment = new Community();
                break;
            case R.id.logOut:
                //sessionManager.logoutUser();
                break;
        }

        if (fragment != null){
            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
        }
        return fragment;
    }
}

MainActivity - PageAdapter

private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position){
            case 0:
                fragment = mainFragment;
                break;
            case 1:
                fragment = listChat;
                break;
            case 2:
                fragment = tchat;
                break;
        }
        return fragment;
    }

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

0 个答案:

没有答案