3
android.app.Fragment android.app.FragmentManager.findFragmentByTag(java.lang.String)' 
on a null object reference

I had found numerous posts with this issue here on SO, but none of them helped me.

It happens relatively rarely (1/100). The place where I call the getFragmentManager() is on the main thread, when a button is clicked :

My flow is simple , first I have fragment A, then add on it several fragments until I get to fragment X. Once a button is clicked in a custom view class that is instantiated and held in the fragment X, I call :

fragment A.getInstance().showLoadBar();

in fragment A the showLoadBar calls a fragment just for displaying loading, and it uses the getFragmentManager() there.

Most of the time it works great, and I am not able to recreate this crash. But I can see that this crash does happen sometimes.

BVtp
  • 2,308
  • 2
  • 29
  • 68
  • Try to put this in Handler with at least some sec. delay.. – Dileep Patel Jan 27 '17 at 08:33
  • Do you call `getFragmentManager()` in the `Activity` or the `Fragment`? – gi097 Jan 27 '17 at 08:36
  • @GiovanniTerlingen the fragment – BVtp Jan 27 '17 at 08:58
  • 1
    Why is your fragment X asking fragment A to do some work(especially as you're adding another fragment) and not the activity which is the owner of all those fragments? Seeing that getInstance() call I would assume that in some conditions you end up with a dead instance of A which will will not have a valid FragmentManager attached to it. You should rethink your logic. – user Jan 27 '17 at 09:58
  • Well actually fragment X isn't the one who's asking. It's a custom view class that fragment X instantiates. – BVtp Jan 27 '17 at 10:19
  • but thank you, I should definitely rethink this logic. Either just pass fragment x to the custom view to call the loading bar, or just use the activity. Probably better though to use the activity ,right? – BVtp Jan 27 '17 at 10:21
  • 1
    Use the activity, for example you could implement a callback system through an interface(like here https://developer.android.com/training/basics/fragments/communicating.html). – user Jan 27 '17 at 10:41

0 Answers0