SupportMapFragment - cannot cast from Fragment to MapFragment

后端 未结 4 1383
深忆病人
深忆病人 2020-12-14 18:26

Trying to put together a little MapFragment in an activity I\'m building, but am having some trouble getting it all to work. I know that the Maps api and Play services are

相关标签:
4条回答
  • 2020-12-14 19:06

    This is what i had to do, because i was working under level 11;

        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.SupportMapFragment;
        import android.support.v4.app.FragmentActivity;
    
        public class MapaActivity extends FragmentActivity {
    
        private GoogleMap map;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mapa);
    
            map =  ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapa)).getMap();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 19:10

    Change your dependencies to:

    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    
    0 讨论(0)
  • 2020-12-14 19:17

    I can either use getFragmentManager() or getSupportFragmentManager().

    There should be no debate here. If getSupportFragmentManager() is available to you, then you are using the Android Support package's backport of fragments, and this is the method that you must use.

    When I opt for getSupportFragmentManager(), Eclipse doesn't like it and gives me the error "Cannot cast from Fragment to MapFragment".

    That is because you should not be using MapFragment. You are using the Android Support package's backport of fragments, and therefore you must use SupportMapFragment.

    0 讨论(0)
  • 2020-12-14 19:25

    hey simply add v4 jar to your libs folder and add to build then in your Fragment page just import

    import android.support.v4.app.FragmentActivity;

    you may able to extends FragmentActivity

    public class MainActivity extends FragmentActivity implements LocationListener{

    }

    and it also access getSupportFragmentManager();

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

    0 讨论(0)
提交回复
热议问题