Cannot resolve method 'getSupportFragmentManager ( )' inside Fragment
Asked Answered
A

14

57

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

public class PETAcikarangsukatani extends Fragment {
Context context;

private static final LatLng SUKATANI = new LatLng(-6.171327, 107.178108);
private static final LatLng WRPOJOK = new LatLng(-6.222411, 107.162158);
private static final LatLng PILAR = new LatLng(-6.257033, 107.156472);
private static final LatLng CIKARANG = new LatLng(-6.256073, 107.143984);


GoogleMap googleMap;
final String TAG = "PathGoogleMapActivity";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_path_google_map, container, false);
    context = rootView.getContext();

    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.map);
    googleMap = fm.getMap();
Allergist answered 11/12, 2014 at 14:46 Comment(3)
Map is a fragment or is a container?Heng
Possible duplicate of How can I access getSupportFragmentManager() in a fragment?Congo
here it is explained properly youtube.com/watch?v=JWk3PXV6O98Cherry
S
97

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.

Seiden answered 11/12, 2014 at 14:47 Comment(3)
what does it mean, but still not running ?Seiden
also you have to change in fragmrnt xml, from: android:name="com.google.android.gms.maps.SupportMapFragment" to android:name="com.google.android.gms.maps.MapFragment"Inseverable
Consider using getChildFragmentManager() inside a fragment instead of getFragmentManager() or getSupportFragmentManager().Ditter
G
50

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.

Goldschmidt answered 16/6, 2015 at 6:12 Comment(1)
This is an importante note: you must add v4 android support to work.Rowland
E
41

Extends FragmentActivity instead of Activity

Emmittemmons answered 30/6, 2015 at 12:37 Comment(5)
As you can see, the topicstarter was asking about Fragment issue not Activity one.Officious
You saved my day. Thank you very much :)Instigate
Oops! you didn't directly answer the asker question, but mine! you solved it. thank you +1Embryonic
you can also use AppCompatActivityWhacking
Thanks! It's working. My class has been inherited from FragmentActivity and compilation ended fine.Riva
K
18

Use getActivity().getSupportFragmentManager()

Kincardine answered 15/5, 2015 at 17:50 Comment(0)
C
16

Replace getSupportFragmentManager() with getFragmentManager() if you are working in api 21. OR If your app supports versions of Android older than 3.0, be sure you've set up your Android project with the support library as described in Setting Up a Project to Use a Library and use getSupportFragmentManager() this time.

Clos answered 26/6, 2015 at 8:18 Comment(1)
Welcome to StackOverflow. Maybe you want to read our editing help to improve your post formatting (esp. section on Code Spans).Huntingdonshire
C
8

Also you can use AppCompatActivity instead of Activity.
Then getSupportFragmentManager() will work.
And also not forget to make your App theme extend AppCompat theme in this case

Calorimeter answered 16/3, 2016 at 9:55 Comment(0)
E
5

As per the documentation, getSupportFragmentManager() is present only inside AppCompatActivity class. It is not present inside Activity class. So make sure your class extends AppCompatActivity class.

public class MainActivity extends AppCompatActivity {
}
Electrostatics answered 12/7, 2017 at 13:7 Comment(0)
S
3

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+

Seemly answered 11/12, 2014 at 14:54 Comment(0)
G
3

getFragmentManager()

just try this.it worked for my case

Gaylordgaylussac answered 21/8, 2016 at 16:38 Comment(0)
B
2

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.

Burin answered 22/5, 2015 at 6:36 Comment(0)
I
1

If you're getting "Cannot resolve method getSupportFragmentManager()", try using

this.getSupportFragmentManager()

It works for me when dealing with DialogFragments in android

Ingathering answered 6/5, 2015 at 15:24 Comment(0)
O
1

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.

Overstep answered 31/7, 2018 at 4:32 Comment(0)
W
1

For me I made sure to import v4 like this:

import android.support.v4.app.DialogFragment;

Then used:

getActivity().getFragmentManager()
Whiffletree answered 15/2, 2019 at 23:22 Comment(0)
J
0

I tried all above, but none working

Finally tried this my own

getBaseActivity().getFragmentManager()

and is working .. :)

Jeremiad answered 28/6, 2016 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.