Non-convertible type error from fragment to SupportMapFragment

So I have a fragment class:

public class MyMapFragmentActivity extends Fragment implements LocationListener,
        GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener {

      

Inside it I have a SupportMapFragment defined:

private SupportMapFragment fragment;

      

I am trying to use findFragmentById:

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getChildFragmentManager();
    fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.content_frame, fragment).commit();
    }
}

      

Line

fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);

      

Even though "fragment" is a SupportMapFragment this gives an irreversible type error: cannot use android.app.fragment for com.google.android.gms.maps.SupportMapFragment!

I am using fragment layout:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />

      

+3


source to share


2 answers


If you are using a compatibility library - use getSupportFragmentManager().findFragmentById(R.id.fragment);

, otherwise extend from android.app.Fragment

instead of android.support.v4.app.fragment

.



+4


source


You are mixing support snippets and no support fragment manager. Use SupportFragmentManager instead.



+2


source







All Articles