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();
Irwans
  • 583
  • 1
  • 4
  • 4

14 Answers14

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.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • what does it mean, *but still not running* ? – Blackbelt Dec 11 '14 at 14:55
  • 1
    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" – Rahul Sharma Jun 06 '15 at 06:40
  • 1
    Consider using getChildFragmentManager() inside a fragment instead of getFragmentManager() or getSupportFragmentManager(). – ngaspama Nov 01 '19 at 13:47
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.

Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
41

Extends FragmentActivity instead of Activity

Vicky
  • 5,098
  • 2
  • 33
  • 31
18

Use getActivity().getSupportFragmentManager()

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Gabriel Wamunyu
  • 734
  • 9
  • 25
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.

rminaj
  • 560
  • 6
  • 28
Aman
  • 161
  • 1
  • 4
  • Welcome to StackOverflow. Maybe you want to read [our editing help](http://stackoverflow.com/editing-help) to improve your post formatting (esp. section on [Code Spans](http://stackoverflow.com/editing-help#code)). – user1438038 Jun 26 '15 at 08:29
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

Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54
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 {
}
Siddarth Kanted
  • 5,738
  • 1
  • 29
  • 20
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+

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
3

getFragmentManager()

just try this.it worked for my case

Venkat Vinay
  • 89
  • 2
  • 5
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.

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
Sazid Ali
  • 63
  • 1
  • 8
1

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

this.getSupportFragmentManager()

It works for me when dealing with DialogFragments in android

Jay Lin
  • 872
  • 6
  • 7
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.

Vijay Kumar Kanta
  • 1,111
  • 1
  • 15
  • 25
1

For me I made sure to import v4 like this:

import android.support.v4.app.DialogFragment;

Then used:

getActivity().getFragmentManager()
Marc Alexander
  • 761
  • 11
  • 24
0

I tried all above, but none working

Finally tried this my own

getBaseActivity().getFragmentManager()

and is working .. :)

Krishan Kumar Mourya
  • 2,207
  • 3
  • 26
  • 31