my problem was in getCount.
The method is trying to return fragments.size when no fragments were actually added.
removing it fixes the problem
Question
I'm trying to create a quiz programme where the users swipe between questions. The fragments need to be dynamic so i can load random questions and they need to be able to report back to the main activity so i can check whether they were right or wrong at the end.
My first problem im trying to attack are making the fragments dynamic. I have code below which creates static fragments. I send in a number (hard coded at the moment but it will be random) Each instance/fragment is saved in an array
My problem is when I try to create the fragments dynamically in the getItem method the programme falls over. Can anybody direct me... (Commented out static working example)
Also see: Android layout using Swipe View and Tile Strip
public class MyPagerAdapter extends FragmentPagerAdapter
{
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm)
{
super(fm);
/*
fragments.add(new QuestionScreen(1));
fragments.add(new QuestionScreen(2));
fragments.add(new QuestionScreen(3));
fragments.add(new QuestionScreen(4));
fragments.add(new QuestionScreen(5));
fragments.add(new QuestionScreen(6));
fragments.add(new QuestionScreen(7));
fragments.add(new QuestionScreen(8));
fragments.add(new QuestionEnd());
fragments.add(new QuestionStart());
fragments.add(new QuestionEnd());
*/
Log.w("he","hehehe");
}
@Override
public Fragment getItem(int position)
{
if (position == 0) { // change to switch statement
return new QuestionScreen(1);
} else if (position == 1) {
return new QuestionScreen(2);
} else if (position == 2) {
return new QuestionScreen(3);
} else if (position == 3) {
return new QuestionScreen(4);
} else {
return new QuestionEnd();
}
//Log.w("se","sesese");
//return fragments.get(position);
}
@Override
public int getCount()
{
Log.w("pe","pepepe");
return fragments.size();
}
}
Solution
my problem was in getCount.
The method is trying to return fragments.size when no fragments were actually added.
removing it fixes the problem