Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-fragmentactivityfragmentstatepageradapter

Android ViewPager get view nullpointer exception


I have support.v4 ViewPager, which I fill with fragments, by using FragmentStatePagerAdapter.

The displaying of the fragments in ViewPager works as expected, but the problem is getting the fragment views when calling the getView() function in the FragmentActvitiy class. I can access all the 15 fragments and get arguments, etc.., but when calling getView() function on fragment I get a nullpointer exception, becase the third fragment getView() is null.

MyPageAdapter adapter = (MyPageAdapter) pager.getAdapter();
Fragment fragment;

for (int i=0; i<15; i++) {
    fragment = adapter.getItem(i);
    View v = fragment.getView();
    System.out.println("--- FRAGMENT = " + fragment + "   VIEW = " + v);
}

Output:

--- FRAGMENT = MyPrvaPomocFragment{40ede4c8 #0 id=0x7f06000d}   VIEW = android.support.v4.app.NoSaveStateFrameLayout{40f11cb0 V.E..... ......I. 0,0-540,850}
--- FRAGMENT = MyPrvaPomocFragment{40edf7c0 #1 id=0x7f06000d}   VIEW = android.support.v4.app.NoSaveStateFrameLayout{40f16f80 V.E..... ......ID 540,0-1080,850}
--- FRAGMENT = MyPrvaPomocFragment{40ee0998}   VIEW = null

FragmentStatePageAdapter class code:

public class MyPageAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> fragments;

    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }

    @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }
}

Fragment class code:

public class MyPrvaPomocFragment extends Fragment {

    public static final String EXTRA_QUESTION_NUMBER = "EXTRA_QUESTION_NUMBER";
    public static final String EXTRA_QUESTION = "EXTRA_QUESTION";
    ...    

    public static final MyPrvaPomocFragment newInstance(String questionNumber, String question, ..., int correctAnswer) {
        MyPrvaPomocFragment f = new MyPrvaPomocFragment();

        Bundle bdl = new Bundle(1);
        bdl.putString(EXTRA_QUESTION_NUMBER, questionNumber);
        bdl.putString(EXTRA_QUESTION, question);
        bdl.putInt(EXTRA_CORRECT_ANSWER, correctAnswer);
        ...
        f.setArguments(bdl);

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String question = getArguments().getString(EXTRA_QUESTION);
        String firstAnswer = getArguments().getString(EXTRA_FIRST_ANSWER);
        ...

        View v = inflater.inflate(R.layout.fragment_prva_pomoc, container, false);

        TextView textViewQuestion = (TextView) v.findViewById(R.id.textViewQuestion);
        CheckBox checkBox1 = (CheckBox) v.findViewById(R.id.checkBox1);
        CheckBox checkBox2 = (CheckBox) v.findViewById(R.id.checkBox2);
        ...

        textViewQuestion.setText(question);
        checkBox1.setText(firstAnswer);
        checkBox2.setText(secondAnswer);

        return v;
    }
}

FragmentActivity class code:

public class KvizPrvaPomoc extends FragmentActivity {

    ViewPager pager;
    MyPageAdapter pageAdapter;    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kviz_prva_pomoc);

        pager = (ViewPager)findViewById(R.id.viewpagerquiz);              
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), getFragments());
        pager.setAdapter(pageAdapter);
    }  

    private List<Fragment> getFragments(){
        String question;
        String firstAnswer;
        String secondAnswer;
        ...

        List<Fragment> fragmentList  = new ArrayList<Fragment>();

        for (int j=0; j<someList.size(); j++)  { 
            ...  
            fragmentList.add(MyPrvaPomocFragment.newInstance(questionNumber, question, firstAnswer, ...));
        }

        return fragmentList;
    }
}

I have read through numerous SO questions/answers, but I have no idea what could be wrong.


Solution

  • This is the expected behavior; FragmentStatePagerAdapter destroys fragments once you scroll more than one fragment away from them in either direction. This is done to conserve memory.