3

i have seen the relative post to my issue but, it's not solve my probleme. I'm trying to get a swipe view with 3 fragment. In the main fragment i have this:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View vue = inflater.inflate(R.layout.fragment_conversions, container, false);

    ConversionsPagerAdapter cPageAdapter = new ConversionsPagerAdapter(getActivity().getSupportFragmentManager());
    ViewPager vp = vue.findViewById(R.id.conversionsPager);
    cPageAdapter.getItem(0);
    vp.setAdapter(cPageAdapter);


   Button convKmhKts = (Button) vue.findViewById(R.id.convKmhKts);
   convKmhKts.setText("...");
...

Here is the class of my FragmentStatePagerAdapter:

public class ConversionsPagerAdapter extends FragmentStatePagerAdapter{
    public ConversionsPagerAdapter(FragmentManager fm) {
        super(fm);
        Log.i("ARKA", "FragmentStatePagerAdapter CONSTRUCTOR");
    }

    @Override
    public Fragment getItem(int i) {
        return ConversionFragment.newInstance(i);
    }

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

And finnaly the Fragment which display the Tab:

public class ConversionFragment extends Fragment {
    public static final String ID_CONVERSION = "id_conversion";

    public static ConversionFragment newInstance(int pos) {
        ConversionFragment stf = new ConversionFragment();
        Bundle args = new Bundle();
        args.putInt("pos", pos);
        stf.setArguments(args);
        return stf;
    }


    public ConversionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.

        Bundle args = getArguments();
        int idConversion = args.getInt(ID_CONVERSION);
        Log.i("idConversion", String.valueOf(idConversion));
        int idVue;

        switch (idConversion){
            case 0: idVue = R.layout.fragment_conversions_vitesse;
                break;
            case 1: idVue = R.layout.fragment_conversions_vitesse;
            default: idVue = R.layout.fragment_conversions_vitesse;
                break;
        }

        View rootView = inflater.inflate(idVue, null);
        return rootView;
    }
}

The fact is, the getItem() is never called. I try to change getActivity().getSupportFragmentManager() by getChildFragmentManager(), but it seem it's not the good way... When i'm trying to acces my button i get this:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

At the line where i'm using the button.

Arka-57
  • 41
  • 5

3 Answers3

2

If you use ViewPager inside fragment you have to use chield fragment manager so try replace getActivity().getSupportFragmentManager() with getChildFragmentManager(). If you use one fragment manager for all stuff, it will bring some bugs to you.

For more information

Onregs
  • 420
  • 3
  • 16
  • Thanks for your answer. I alredy try it, and i have the same issue: a nullpointer exception with the ChildManager... – Arka-57 Aug 12 '17 at 04:23
0

You are doing wrong here

cPageAdapter.getItem(0);
vp.setAdapter(cPageAdapter);

Set adapter to view pager first and then try to call get item method.

vp.setAdapter(cPageAdapter);
cPageAdapter.getItem(0);
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
  • Hi and thanks for your answer, it was just for a test, i forget to revome this line, but i will try your idera. – Arka-57 Aug 12 '17 at 04:24
0

Ok, the probleme was that i try to acces a component which is not inflated:

convKmhKts.setText("...");

I acces to it in the child fragment and now it's ok....

Arka-57
  • 41
  • 5