Home » Java » Android googleMap Map does not show, only grid, sometimes

Android googleMap Map does not show, only grid, sometimes

Posted by: admin December 28, 2021 Leave a comment


So, I have been working on getting this up and running for some time now. Problem is I have my map key from Google placed correctly in main.xml, but I can’t seem to get a provider information as the map does not display at all. I have run it a couple of times and the fail us at the same place. I tried to debug and noticed that there is a provider string returned, however, after that a null.

The code fails at

protected void onResume() {
            .requestLocationUpdates(getBestProvider(), 1000, 1, this);

with the following error message.

08-20 05:14:43.573: E/AndroidRuntime(239): Uncaught handler: thread main exiting due to uncaught exception
08-20 05:14:43.583: E/AndroidRuntime(239): java.lang.RuntimeException: Unable to resume activity {com.shawnbe.mallfinder/com.shawnbe.mallfinder.MallFinderActivity}: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2965)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.os.Looper.loop(Looper.java:123)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.main(ActivityThread.java:4363)
08-20 05:14:43.583: E/AndroidRuntime(239):  at java.lang.reflect.Method.invokeNative(Native Method)
08-20 05:14:43.583: E/AndroidRuntime(239):  at java.lang.reflect.Method.invoke(Method.java:521)
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-20 05:14:43.583: E/AndroidRuntime(239):  at dalvik.system.NativeStart.main(Native Method)
08-20 05:14:43.583: E/AndroidRuntime(239): Caused by: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.shawnbe.mallfinder.MallFinderActivity.onResume(MallFinderActivity.java:61)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Activity.performResume(Activity.java:3763)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)
08-20 05:14:43.583: E/AndroidRuntime(239):  ... 12 more

Occasionally, the code seems launches the grid but no map. While on other runs, it simply fails out.

Below is my activity code.

package com.shawnbe.mallfinder;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MallFinderActivity extends MapActivity implements
        android.location.LocationListener {
    /** Called when the activity is first created. */

    private MapController mapController;
    private MapView mapView;
    private android.location.LocationManager locationManager;
    private GeoPoint currentPoint;
    private Location currentLocation = null;
    private TextView latituteField;
    private TextView longitudeField;
    private String bestProvider;

    public void onCreate(Bundle savedInstanceState) {
        MapView mapView = (MapView) findViewById(R.id.mapView);
        Location location;
        if(bestProvider !=null)
        location = locationManager.getLastKnownLocation(bestProvider);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(
        MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,
        LocalGeoPoints loc = new LocalGeoPoints();
        for (OverlayItem a : loc.overlayitems) {


    protected void onResume() {
                .requestLocationUpdates(getBestProvider(), 1000, 1, this);

    public void getLastLocation() {
        String provider = getBestProvider();
        currentLocation = locationManager.getLastKnownLocation(provider);
        if (currentLocation != null) {
        } else {
            Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG)

    public void animateToCurrentLocation() {
        if (currentPoint != null) {

    public String getBestProvider() {
        locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        bestProvider = locationManager.getBestProvider(criteria, false);

        return bestProvider;

    public void setCurrentLocation(Location location) {
        int currLatitude = (int) (location.getLatitude() * 1E6);
        int currLongitude = (int) (location.getLongitude() * 1E6);
        currentPoint = new GeoPoint(currLatitude, currLongitude);
        currentLocation = new Location("");
        currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
        currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);

    protected boolean isRouteDisplayed() {

        return false;

    /* Remove the locationlistener updates when Activity is paused */
    protected void onPause() {

    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());

    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,


    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub



And below is my main.xml ccomplete with the key I obtained from the google map registeration site.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

The image below is all I get


https://developers.google.com/maps/documentation/android/mapkey – official information, what to do.

Step by step.

1) Create keystore

2) Generating hash MD5 from your keystore

3) Sign in here : https://developers.google.com/android/maps-api-signup

4) Add key to your application

Good luck!


In onCreate you should init locationManager field. Move

locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);

string from getBestProvider() method to your onCreate()

Look at your stacktrace. You have only one place where NPE can potentially occurred

. . . 
08-20 05:14:43.583: E/AndroidRuntime(239): Caused by: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.shawnbe.mallfinder.MallFinderActivity.onResume(MallFinderActivity.java:61)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)


The reason you are getting a NullPointer is that you never set locationManager before you use it in onResume().

onCreate() is going to get called where you have getBestProvider() commented out. Then onResume() gets called and that uses locationManager. Although you have yet another even more complicated situation here. I’ll see if I can walk through procedurally what code will execute in onResume()…

  1. super.onResume()
  2. locationManager.requestLocationUpdates() <– it’ll probably go look up this method and get you a null pointer… we’ll continue if locationManager weren’t null
  3. locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE); <– now you’re changing the object referenced from step 2…

If I were you I’d simplify things, only set the locationManager in onCreate() (as a general programming practice you want to limit the number of side effects in methods, what I mean is in getBestProvider(), you should just return the best provider, and try not to do anything else to your state, or you get crazy unexpected things outside that method). If you’re still getting some madness, I would just put that first line of getBestProvider() in onResume()

protected void onResume() 
    locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);

You probably don’t want to leave it that way, but it will ensure that your problem is that locationManager is null in onResume()

Good luck.


if you have valid map key then check whether you are getting an instance of locationManager into onResume i guess you have to instantiate location manager in onResume() .

 locationManager = (android.location.LocationManager)getSystemService(Context.LOCATION_SERVICE);


You will need to center the map view and set a reasonable zoom level. Otherwise, the map view may show some random area.

    new GeoPoint((int)(25.76032 * 1E6),  (int)(-80.213928 * 1E6)));
//Set a reasonable zoom level


NullPointerException is because of you are accessing null object of locationmanager.


create apk in release mode and run this apk file,because google map will display when apk in release mode.


Please Update your Google map API key, previous API key was expired so in this circumstance you are not able to see Google Map. https://developers.google.com/android/maps-api-signup


Make sure you initialize it before using it.

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);


probabilly there is also a mapkey problem:

if you run your application in debug mode for testing, you have to create a mapkey using the default keystore for debug (for example on linux you can find it at ~/.android/debug.keystore) and insert this mapkey in code and use it untill you debug your app.

if you want to create an apk and run it (for example to publish your apk on market), you have to create a new mapkey using your personal keystore, and change it from your code before to generate the apk


The MAP API requires a valid key to work properly.

To get the api key for your application you need to follow the steps given below.

  1. Generate the certification fingerprint of your application using the keytool command.

    keytool -list -keystore ~/.android/debug.keystore
    Example: 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98

  2. Using the fingerprint value you can generate the map api key from the link given below



  1. when you want to release the application, The same maps api key can’t be used. you must use the release keystore instead of the debug keystore (.android/debug.keystore). Generate the fingerprint using the command given above and use it to generate the maps api key for production use.
  2. The keytool binary can be found in your system jre bin folder.

C:\Program Files\Java\jdk1.7.0_06\jre\bin


locationManager is null, there is no discussion about it, did you specify it in manifest.xml?