Skip to content Skip to sidebar Skip to footer

Google Maps Fragment Doesn't Load

I want to load a Google Maps object and create some listener: I created this class: import java.lang.ref.WeakReference; import com.google.android.gms.maps.CameraUpdateFactory; imp

Solution 1:

In the XML file, use class="com.google.android.gms.maps.SupportMapFragment".

In you main class, use:

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

Instead of:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

Solution 2:

To add SupportMapFragment to your project you need to add google-support-v4 library and google map libraries. You can read about how to do that in a blog post I wrote, Google Maps API V2.

Solution 3:

FragmentActivity is provided by the Android Support Library v4 to allow Android platforms before Android 3 (Honeycomb) to use fragments. With Honeycomb and later, fragments are supported using by the standard Activity class

The catch is, depending on the activity class you choose, you cannot use the same fragment class:

  • with android.support.v4.app.FragmentActivity, you muse use android.support.v4.app.Fragment
  • with android.app.Activity, you must use android.app.Fragment

Note the package difference.

Now, with respect to Google Maps v2 for Android, you have two Fragment implementations:

  • MapFragment which extends android.app.Fragment for Honeycomb and later
  • SupportMapFragment which extends android.support.v4.app.Fragment for support of the older Android versions

So, as you use FragmentActivity, you must use the SupportMapFragment implementation for your application.

Post a Comment for "Google Maps Fragment Doesn't Load"