Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate Fragment

Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public

Is it because my Fragment is not a static class? Is it because my Fragment is an inner class?

If I make my Fragment a static class, all my references to findViewById fail, which means a LOT of refactoring.

How can I solve this without turning my inner Fragment into a static class?

like image 771
hunterp Avatar asked Aug 10 '11 19:08

hunterp


1 Answers

is it because my Fragment is an inner class

If your fragment is an inner class, it must be a static inner class. Ideally, it's a standalone public Java class.

if I make my Fragment a static class, all my references to findViewById fail, which means a LOT of refactoring

You needed to do that refactoring anyway. Widgets are now owned by the fragment, not the activity. Fragments should know as little as possible about what activity contains them, so they can be shuffled between different activities as needed to support phones, tablets, TV, etc.

How can I solve this without turning my inner Fragment into a static class??

You make it a standalone public Java class.

like image 197
CommonsWare Avatar answered Oct 06 '22 14:10

CommonsWare