Recently on Blog
Android 2.x NoClassDefFoundError caused by Fragment or FragmentManager
The most confusing part for me about the problems with Navigation Drawer was that the app worked smoothly on all devices that were in my reach; but all of them was on Android 4.0 or higher. Only after I debugged the code on real Android 2.x device I was able to look what's wrong inside the code (well for some reason I do not debug my apps on emulators but on real devices).
What about ProGuardThe net is full of solutions based on playing with ProGuard configuration files because it might look like the requiered class has been removed by Proguard optimalization. I've been trying to tweak my ProGuard config file to keep all the requiered stuff in my package but it didn't lead to any success nor change.
Debugging the real problemWhen I attached the debugging Android device (Samsung Galaxy Mini) with Android 2.5 it turned out clearly that the app crashes when it is creating instance of FragmentManager class. Now what helped me out was hovering over the FragmentManager class in the Eclipse to see that this class is imported from android.app package
and it is obvious this package requieres API level 11. But the Android 2.x is below that level and since then there was no doubt that this was the real case why it's been crashing on startup. Fixing the problem was easy, removing two imports
and adding two new ipomrts from support library
fixed the problem along with calling
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager;
instead of API level 11 code
FragmentManager fragmentManager = getSupportFragmentManager();
Now the biggest question for me is why Eclipse allowed me to use API level 11 code even though I'm targetting my app to minimum API level 7.
FragmentManager fragmentManager = getFragmentManager();
invalidateOptionsMenu() and API < 11FragmentManager was not only the one thing that requiered fixing the Navigation Drawer functionality. Another crashes were caused by calling the method
This method is available only in API starting from level 11 and if you want to use it on lower levels, you have two choices how to replace your code:
for example if calling from MainActivity call
Or just call
That was it. Let me know your feedback in comments down below. Thank you!
24. 5. 2014 9:11:33, Shuvro Pal
Very good solutions that work for me. Appreciate your sharing.