Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 671
Irwans Avatar asked Dec 11 '14 14:12

Irwans


People also ask

What is the use of getSupportFragmentManager?

getSupportFragmentManager and getChildFragmentManager FragmentManager is class provided by the framework which is used to create transactions for adding, removing or replacing fragments. getSupportFragmentManager is associated with Activity consider it as a FragmentManager for your Activity .

What is difference between getSupportFragmentManager vs Getfragmentmanager?

Basically, the difference is that Fragment's now have their own internal FragmentManager that can handle Fragments.


13 Answers

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.

like image 133
Blackbelt Avatar answered Oct 05 '22 22:10

Blackbelt


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.

like image 22
Md Sufi Khan Avatar answered Oct 05 '22 22:10

Md Sufi Khan


Extends FragmentActivity instead of Activity

like image 35
Vicky Avatar answered Oct 05 '22 23:10

Vicky


Use getActivity().getSupportFragmentManager()

like image 45
Gabriel Wamunyu Avatar answered Oct 06 '22 00:10

Gabriel Wamunyu


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.

like image 27
Aman Avatar answered Oct 05 '22 23:10

Aman


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

like image 22
Leonid Ustenko Avatar answered Oct 06 '22 00:10

Leonid Ustenko


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 {
}
like image 36
Siddarth Kanted Avatar answered Oct 05 '22 23:10

Siddarth Kanted


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+

like image 33
Marcin Orlowski Avatar answered Oct 05 '22 22:10

Marcin Orlowski


getFragmentManager()

just try this.it worked for my case

like image 29
Venkat Vinay Avatar answered Oct 05 '22 22:10

Venkat Vinay


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.

like image 44
Sazid Ali Avatar answered Oct 05 '22 23:10

Sazid Ali


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

this.getSupportFragmentManager()

It works for me when dealing with DialogFragments in android

like image 31
Jay Lin Avatar answered Oct 05 '22 23:10

Jay Lin


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.

like image 29
Vijay Kumar Kanta Avatar answered Oct 05 '22 22:10

Vijay Kumar Kanta


For me I made sure to import v4 like this:

import android.support.v4.app.DialogFragment;

Then used:

getActivity().getFragmentManager()
like image 28
Marc Alexander Avatar answered Oct 06 '22 00:10

Marc Alexander