[android] Cannot resolve method 'getSupportFragmentManager ( )' inside Fragment

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();

This question is related to android google-maps android-fragments fragment

The answer is


getFragmentManager()

just try this.it worked for my case


Extends FragmentActivity instead of Activity


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.


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 {
}

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


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.


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.


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

this.getSupportFragmentManager()

It works for me when dealing with DialogFragments in android


Use getActivity().getSupportFragmentManager()


For me I made sure to import v4 like this:

import android.support.v4.app.DialogFragment;

Then used:

getActivity().getFragmentManager()

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.


I tried all above, but none working

Finally tried this my own

getBaseActivity().getFragmentManager()

and is working .. :)


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+


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to google-maps

GoogleMaps API KEY for testing Google Maps shows "For development purposes only" java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion How to import JSON File into a TypeScript file? Googlemaps API Key for Localhost Getting "Cannot call a class as a function" in my React Project ERROR: Google Maps API error: MissingKeyMapError This page didn't load Google Maps correctly. See the JavaScript console for technical details Google Maps API warning: NoApiKeys ApiNotActivatedMapError for simple html page using google-places-api

Examples related to android-fragments

FragmentActivity to Fragment How to start Fragment from an Activity How to use data-binding with Fragment In android how to set navigation drawer header image and name programmatically in class file? Android Fragment onAttach() deprecated How to convert any Object to String? Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which? Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments java.lang.IllegalStateException: Fragment not attached to Activity java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference

Examples related to fragment

FragmentActivity to Fragment Handling back button in Android Navigation Component android.content.Context.getPackageName()' on a null object reference Cannot resolve method 'getSupportFragmentManager ( )' inside Fragment How to move from one fragment to another fragment on click of an ImageView in Android? Android - save/restore fragment state Intent from Fragment to Activity Get the Application Context In Fragment In Android? Add Items to ListView - Android Using onBackPressed() in Android Fragments