The problem is most likely that you're using an old Fragment that has been detached from your Activity.
So, the first time you create your Fragment, it is attached to your activity. All is good. Then when you change tab, your fragment might or might not be detached from the activity. When you tab back to it, the old fragment may be detached from the activity and so getActivity()
returns null.
This can happen if you're trying to keep references to your Fragments, rather than accessing them via the FragmentManager
.
It can also happen if your adapter is returning a reference to a fragment rather than a new fragment. I've fallen into this trap.
(Posting the code where you create your fragments might help)
Edit
Maybe have a look at this and how they create add their ActionBar listeners. You need scope to your Activity. The way they do it is to define the listener in the Activity/Fragment (via implementing an interface) and then attach it to the Tab. This will give you scope and is probably a more stable way of doing things.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…