Thursday, November 7, 2013

Google Maps Android API v2 : mapFragment.getMap() returns null


If mapFragment.getMap() returns null then map is not verified from the GooglePlayServices.

Google Play Services
Give apps more features to attract users on a wider range of devices. With Google Play services, our app can take advantage of the latest, Google-powered features such as Maps, Google+, and more, with automatic platform updates distributed as an APK through the Google Play store.

This type of exception occurs when there are no proper GooglePlayServices updates.
We can check using GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) method, Which returns status code indicating whether there was an error or not.

public static int isGooglePlayServicesAvailable (Context context)
Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.
Returns
status code indicating whether there was an error. Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID, DATE_INVALID.

Map the output of GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) return value with ConnectionResult class Constants. We can find the response of GooglePlayServices.

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

if (map != null) {
// The Map is verified. It is now safe to manipulate the map.
}else {
int checkGooglePlayServices = GooglePlayServicesUtil
              .isGooglePlayServicesAvailable(this);
       if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
              // google play services is missing!!!!
              /*
               * Returns status code indicating whether there was an error.
               * Can be one of following in ConnectionResult: SUCCESS,
               * SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED,
               * SERVICE_DISABLED, SERVICE_INVALID.
               */
              GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices,
                     this, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
       }
}

Below are the error codes used by GooglePlayServicesClient.OnConnectionFailedListener

Constants
int
The device date is likely set incorrectly.
int
The application is misconfigured.
int
The Drive API requires external storage (such as an SD card), but no external storage is mounted.
int
An internal error occurred.
int
The client attempted to connect to the service with an invalid account name specified.
int
The application is not licensed to the user.
int
A network error occurred.
int
Completing the connection requires some form of resolution.
int
The installed version of Google Play services has been disabled on this device.
int
The version of the Google Play services installed on this device is not authentic.
int
Google Play services is missing on this device.
int
The installed version of Google Play services is out of date.
int
The client attempted to connect to the service but the user is not signed in.
int
The connection was successful.


Note: Do Google Play Services Verification when we are using services of Google.

Origin References:

No comments :

Post a Comment