마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 프래그먼트에 getSupportFragmentManager에러

0 추천
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.home, container, false);

    /*Fragment내에서는 mapView로 지도를 실행*/
    mapView = (MapView)v.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    mapView.onResume();
    mapView.getMapAsync(this);

    getActivity().getWindow().setFlags
(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mLayout = v.findViewById(R.id.layout_main);

    locationRequest = new LocationRequest()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(UPDATE_INTERVAL_MS)
            .setFastestInterval(FASTEST_UPDATE_INTERVAL_MS);

    LocationSettingsRequest.Builder builder =
            new LocationSettingsRequest.Builder();

    builder.addLocationRequest(locationRequest);

    mFusedLocationClient = LocationServices.
getFusedLocationProviderClient(getActivity());

    SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()// 이부분이 에러가 납니다.
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    return v;
}
익명사용자 님이 2019년 9월 5일 질문

1개의 답변

0 추천
getSupportFragmentManager  는 FragmentActivity 의 메소드입니다.

Fragment에서 부르시니 모르는 함수라고 오류가 날수 밖에 없습니다.

가능하면 콜백등으로 FragmentAcvitiy 로 이벤트를 보내처리하시는게 좋을 듯하며,

구조상 어려우면   FragmentAcvitiy를 넘겨 받아   activitiy.getSupportFragmentManager() 같이 호출 하세요.
익명사용자 님이 2019년 9월 5일 답변
FragmentActivity activitiy = null;
        SupportMapFragment mapFragment = (SupportMapFragment) activitiy.getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return v;

이렇게 해서 에러를 잡았는데 혹시 이렇게 하는 게 맞는 건가요?

(댓글을 최소 12자 이상 입력해야 하네요..-ㅇ-;;)
...