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条回答
Root(大扎)
2楼-- · 2019-01-11 01:12

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+

查看更多
老娘就宠你
3楼-- · 2019-01-11 01:15

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.

查看更多
倾城 Initia
4楼-- · 2019-01-11 01:19

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.

查看更多
做个烂人
5楼-- · 2019-01-11 01:20

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

查看更多
一夜七次
6楼-- · 2019-01-11 01:23

getFragmentManager()

just try this.it worked for my case

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-11 01:24

Extends FragmentActivity instead of Activity

查看更多
登录 后发表回答