Getting Error In The App After Making Custom Fragments
Solution 1:
This is because you have an error when you are trying to add the questions. You are adding the same questions again and again.
Change the below code :
currentQ = quesList.get(qid);
List<FirstFragment> fragments =new Vector<FirstFragment>();
fragments.add(FirstFragment.newInstance(0,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
fragments.add(FirstFragment.newInstance(1,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
fragments.add(FirstFragment.newInstance(2,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
to
currentQ = quesList.get(qid);
List<FirstFragment> fragments =new Vector<FirstFragment>();
fragments.add(FirstFragment.newInstance(0,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(1,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
qid++;
currentQ = quesList.get(qid);
fragments.add(FirstFragment.newInstance(2,currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()));
Solution 2:
I think your problem originates at these three lines because all those keys are the same.
args.putString("some_option1",radio_1);
args.putString("some_option1",radio_2);
args.putString("some_option1",radio_3);
Some suggestions, try to restructure your adapter filling code like so
quesList = db.getAllQuestions();
for (int page = 0; page < quesList.size(); page++) {
Question currentQ = quesList.get(page);
// Is that page parameter needed in here?
FirstFragment questionFragment = FirstFragment.newInstance(page, currentQ);
fragments.add(questionFragment);
}
mPagerAdapter = new com.example.android.viewpager4.PagerAdapter(this.getSupportFragmentManager(),fragments);
Notice that currentQ
in the newInstance
looks cleaner than currentQ.getQUESTION(),currentQ.getOPTA(),currentQ.getOPTB(),currentQ.getOPTC()
To prevent throwing around a List of Fragment objects, though, you could change the Adapter to take a List of Questions.
publicclassQuestionPagerAdapterextendsFragmentPagerAdapter
{
private List<Question> questions;
publicPagerAdapter(FragmentManager fm, List<Question> questions) {
super(fm);
this.questions = questions;
}
@Overridepublic Fragment getItem(int position)
{
Questionq= questions.get(position);
// Is that int parameter needed here?return FirstFragment.newInstance(position, q);
}
@OverridepublicintgetCount()
{
returnthis.questions.size();
}
}
In both of those cases, you need to update the newInstance
method.
publicstatic FirstFragment newInstance(int page, Question q)
{
FirstFragmentfragmentFirst=newFirstFragment();
Bundleargs=newBundle();
args.putInt("someInt", page); // Is this needed?
args.putString("some_question", q.getQUESTION());
args.putString("some_option1",q.getOPTA());
args.putString("some_option2",q.getOPTB());
args.putString("some_option3",q.getOPTC());
fragmentFirst.setArguments(args);
return fragmentFirst;
}
Then, to make that method look a little cleaner, you could make some public static final String
fields for those argument strings just like you made for the database keys.
For example
publicstaticfinalStringARG_QUESTION="some_question";
publicstaticfinalStringARG_OPT_A="some_option1";
// etc.
Then
args.putString(ARG_QUESTION, q.getQUESTION());
args.putString(ARG_OPT_A, q.getOPTA());
// etc.
And
Bundleargs= getArguments();
if (args != null)
{
question = args.getString(ARG_QUESTION, currentQ.getQUESTION());
radio_1 = args.getString(ARG_OPT_A, currentQ.getOPTA());
// etc.
}
And, finally, making Question
implement Parcelable
would be the best approach for passing data into the Fragment, that way, you don't need to get the currentQ
back out of the database. (Because you are reading the entire database and only defaulting to the qid
of 0 when those arguments are missing).
Post a Comment for "Getting Error In The App After Making Custom Fragments"