Cannot resolve method 'getSupportFragmentManag

2019-01-11 00:28发布

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

13条回答
Lonely孤独者°
2楼-- · 2019-01-11 00:57

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.

查看更多
再贱就再见
3楼-- · 2019-01-11 01:00

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

this.getSupportFragmentManager()

It works for me when dealing with DialogFragments in android

查看更多
趁早两清
4楼-- · 2019-01-11 01:03

I tried all above, but none working

Finally tried this my own

getBaseActivity().getFragmentManager()

and is working .. :)

查看更多
在下西门庆
5楼-- · 2019-01-11 01:04

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.

查看更多
贪生不怕死
6楼-- · 2019-01-11 01:10

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 {
}
查看更多
劳资没心,怎么记你
7楼-- · 2019-01-11 01:12

Use getActivity().getSupportFragmentManager()

查看更多
登录 后发表回答