Arka-57
Arka-57

Reputation: 41

FragmentStatePagerAdapter is not calling getItem()

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.

Upvotes: 3

Views: 1568

Answers (3)

Arka-57
Arka-57

Reputation: 41

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....

Upvotes: 0

Onregs
Onregs

Reputation: 430

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

Upvotes: 2

Wasim K. Memon
Wasim K. Memon

Reputation: 6067

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);

Upvotes: 0

Related Questions