Cannot resolve method 'getSupportFragmentManager ( )' inside Fragment

后端 未结 14 746
别跟我提以往
别跟我提以往 2020-12-01 05:16

I found the message Cannot resolve method \'getSupportFragmentManager ( )\' I want to fragment as activity. because I want to use Maps on the tabs swipe.

pub         


        
相关标签:
14条回答
  • 2020-12-01 05:37

    you should use

    getActivity.getSupportFragmentManager() like
    //in my fragment 
    SupportMapFragment fm = (SupportMapFragment)    
    getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
    

    I have also this issues but resolved after adding getActivity() before getSupportFragmentManager.

    0 讨论(0)
  • 2020-12-01 05:38

    getSupportFragmentManager() is not part of Fragment, so you cannot get it here that way. You can get it from parent Activity (so in onAttach() the earliest) using normal

    activity.getSupportFragmentManager();
    

    or you can try getChildFragmentManager(), which is in scope of Fragment, but requires API17+

    0 讨论(0)
  • 2020-12-01 05:38

    For me I made sure to import v4 like this:

    import android.support.v4.app.DialogFragment;
    

    Then used:

    getActivity().getFragmentManager()
    
    0 讨论(0)
  • 2020-12-01 05:40

    If you're instantiating an android.support.v4.app.Fragment class, the you have to call getActivity().getSupportFragmentManager() to get rid of the cannot-resolve problem. However the official Android docs on Fragment by Google tends to over look this simple problem and they still document it without the getActivity() prefix.

    0 讨论(0)
  • 2020-12-01 05:41

    For my case, I made sure that Fragment class is imported from

    android.support.v4.app.Fragment

    Not from

    android.app.Fragment

    Then I have used

    getActivity().getSupportFragmentManager();

    And now its working. If I use activity instance which I got in onAttach() is not working also.

    0 讨论(0)
  • 2020-12-01 05:42

    Inside a Fragment subclass you have to use getFragmentManager in place of getSupportFragmentManager. You will get the support one if the import are from the support library.

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