makapaka
makapaka

Reputation: 179

Inconvertible type error from fragment to SupportMapFragment

So I have a fragment class:

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

Within it I have a SupportMapFragment defined:

private SupportMapFragment fragment;

I am trying to use the 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();
    }
}

The line

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

Despite the 'fragment' being a SupportMapFragment, this is giving inconvertible type error: cannot cast android.app.fragment to com.google.android.gms.maps.SupportMapFragment !

I'm 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" />

Upvotes: 3

Views: 3374

Answers (2)

Oleg Novosad
Oleg Novosad

Reputation: 2421

If you use compatibility library - use getSupportFragmentManager().findFragmentById(R.id.fragment); otherwise extend from android.app.Fragment instead of android.support.v4.app.fragment.

Upvotes: 4

Gabe Sechan
Gabe Sechan

Reputation: 93569

You're mixing support fragments and the non-support fragment manager. Use the SupportFragmentManager instead.

Upvotes: 2

Related Questions