I’ve been using the Android compatibility Package but i encountered the following issue, it seems that whenever i create a Fragment as an inner static class on my application and try to start that activity the it display the following error
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment org.wr.CreditCardHolderActivity.CreditCardHolderFragment: make sure class name exists, is public, and has an empty constructor that is public
And when i separate the fragment and the activity everything work smoothly, anyone know why? and how can i fix it?
Thanks!
If you have an inner class Fragment like:
Make sure your qualifier is public when the FragmentActivity tries to reinitiate the fragment it doesn’t call it from the concrete class it will handle it from the abstract FragmentActivity, and if your inner class Fragment is private the Activity has no reference to onSaveState, onRestoreState, initialise etc..
privatetopublicfixed it for me!Update:
Have a look at https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/Fragment.java
The
instantiatemethod callsnewInstance()when trying to restore theFragmentfrom a saved state (where the fragment was completely destroyed).The
newInstancemethod requires the class to be publicly accessible, so when defined as an inner class this means it has to bepublicandstaticwhere appropriate.Hope this clears up some future questions.