Fragment already added IllegalStateException with only one copy of fragment

Its not necessary that problem is in with the way you are adding fragments. As i faced similar issue and later when i used debugger to go step by step to find real issue it turned out issue was with views in fragment layout.

I was getting similar exception like yours but real exception was

java.lang.ClassCastException:

It seems sometimes that this is a generic error thrown when something occurred while loading the fragment.

For example: I had this error when inside onCreateView I called findViewById with a wrong id that didn't exist on fragment's layout.

So, make sure your fragment onCreateView's code doesn't has any bug (try commenting it all - only returning the raw inflated view - and see if it still throws this error.


The issue was that I was pre-adding the Fragments to the manager. FragmentPagerAdapter automatically adds all Fragments to the manager in the getItem() method.

Refer to this answer for details on the getItem() method.

Tags:

Android