java.lang.illegalStateException Actionbar Tab must have a callback

after much Goggling and RTFMing I still don't understand how I am supposed to supply a callback in this code so that android will stop throwing an exception at runtime
The error is that Actionbar Tab(s) apparently don't have a callback
Only the code up to line 54 should be applicable?

Resolution If you notice in lines 2 and 3 (49 and 50 in the original)
I tried to add the tabs to the action bar
and then in lines 5 and 6 (52 and 53 in the original)
I tried to add the listener to the tabs

In this case it actually matters what order you try and do things in
Before you add a tab to the action bar it MUST have a listener added to it
otherwise you will get an exemption at run time

ALSO I placed the code for the NavTabListener class in a more sensible place
I placed it in the main body of the DynamicFragment class rather than placing it in the middle of
the onCreate() method
The Finished Code

Ulf Dittmer

Rancher

Posts: 42969

73

posted 2 years ago

Thanks for reporting the solution. It seems indeed unusual that the listeners would have to be added first. Off the top of my head I can't think of other places in the Android API (or the Java API, for that matter) where the listener couldn't be added later.

This is good info! I didn't really believe it, I thought it might be your use of commit() and/or addToBackStack() (both of which are handled for you and if you do yourself, the API Guide says, could give exceptions). But looking further, the API for ActionBar.Tab says the order matters. Very strange indeed.