Unable to instantiate Fragment

后端 未结 12 2045
无人及你
无人及你 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:14

    i have meet this problem you need use full class name : eg: Fragment.instantiate(MainActivity.this, com.XX.yourFragmentName);

    must full class name

    0 讨论(0)
  • 2020-12-24 07:19

    The inner class constructor must be pass in an instance of the outer class. so it is said the compiler cannot find the constructor which has no parameter. so it should be put into static of other java file.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 07:24

    I had this problem as well - turns out it was getting confused because my custom Fragment had a constructor.

    I renamed the constructor method and called the new method instead upon instantiation, and it worked!

    0 讨论(0)
  • 2020-12-24 07:25

    if you don't want to make the inner class static, try to override the method onPause of the dialog fragment like this:

    public void onPause()
    {
      super.onPause();
      dismiss();
    }
    

    so the fragment should be destroyed when the app goes on pause and there is no exception. i tried it and works.

    0 讨论(0)
  • 2020-12-24 07:27

    Your Fragment shouldn't have constructors (see this documentation and its examples).

    You should have a newInstance() static method defined and pass any parameters via arguments (bundle)

    For example:

    public static final MyFragment newInstance(int title, String message)
    {
        MyFragment fragment = new MyFragment();
        Bundle bundle = new Bundle(2);
        bundle.putInt(EXTRA_TITLE, title);
        bundle.putString(EXTRA_MESSAGE, message);
        fragment.setArguments(bundle);
        return fragment ;
    }
    

    And read these arguments at onCreate:

    @Override
    public Dialog onCreate(Bundle savedInstanceState)
    {
        title = getArguments().getInt(EXTRA_TITLE);
        message = getArguments().getString(EXTRA_MESSAGE);
    
        //...
        //etc
        //...
    }
    

    This way if detached and re-attached the object state can be stored through the arguments, much like bundles attached to Intents.

    0 讨论(0)
提交回复
热议问题