Hello,
I'm writing application with NavigationView and now I add to ViewPager on the layout for first Fragment.
Unfortunately, when i switch item in navigation and I come back to previous (with ViewPager) I have exception and application has been stopped.
Below part of code to change navigation item:
Part in mainSchedule Fragment:
And part of class extends from FragmentStatePagerAdapter:
Now I have :
java.lang.IllegalStateException: Fragment already added: Test
When I move Fragment Test to ViewPager class in method getItem(int position) I have:
java.lang.IllegalStateException: No activity
Can anybody explain me this and help me repair it?
I'm writing application with NavigationView and now I add to ViewPager on the layout for first Fragment.
Unfortunately, when i switch item in navigation and I come back to previous (with ViewPager) I have exception and application has been stopped.
Below part of code to change navigation item:
Code:
switch (mNavItemId) {
case R.id.drawer_item_1:
getFragmentManager().beginTransaction().replace(R.id.content, mainSchedule)
.commit();
break;
case R.id.drawer_item_2:
getFragmentManager().beginTransaction().replace(R.id.content, mFirstFragment)
.commit();
break;
case R.id.drawer_item_3:
getFragmentManager().beginTransaction().replace(R.id.content, mSecondFragment)
.commit();
break;
case R.id.drawer_item_4:
getFragmentManager().beginTransaction().replace(R.id.content, mThridFragment)
.commit();
break;
default:
getFragmentManager().beginTransaction().replace(R.id.content, mainSchedule)
.commit();
Code:
View view = inflater.inflate(R.layout.main_schedule, container, false);
mPager = (ViewPager) view.findViewById(R.id.viewpager);
mPagerAdapter = new PageAdapters(getChildFragmentManager());
mPagerAdapter.addFragments(new Test());
mPager.setAdapter(mPagerAdapter);
return view;
Code:
List<Fragment> fragments;
public PageAdapters(FragmentManager fm) {
super(fm);
fragments = new ArrayList<Fragment>();
}
public void addFragments(Fragment fragment) {
fragments.add(fragment);
}
@Override
public Fragment getItem(int position) {
return fragments.get(0);
}
@Override
public int getCount() {
return 2;
}
Now I have :
java.lang.IllegalStateException: Fragment already added: Test
When I move Fragment Test to ViewPager class in method getItem(int position) I have:
java.lang.IllegalStateException: No activity
Can anybody explain me this and help me repair it?