Unable to instantiate Fragment

后端 未结 12 2155
无人及你
无人及你 2020-12-24 06:42

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

Is it because my Fr

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 07:22

    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.

提交回复
热议问题