Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleMaps MapView crashes onTap in Android?

I'm currently writing an app displaying a MapView showing a google map. i used the "hello map view" tutorial from http://developer.android.com/resources/tutorials/views/hello-mapview.html and this works so far: the map is displayed, centered and zoomed correctly, even the OverlayItems i added are displayed correctly.

But when i click on one of those overlay items i added, the application crashes with a null pointer exception:

07-23 16:24:48.167: ERROR/AndroidRuntime(2530): java.lang.NullPointerException
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at     com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:753)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at net.xenonite.wifiloc.act_map$MapItemizedOverlay.onTap(act_map.java:182)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:647)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.View.dispatchTouchEvent(View.java:3709)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:874)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1708)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.os.Looper.loop(Looper.java:123)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at android.app.ActivityThread.main(ActivityThread.java:4595)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at java.lang.reflect.Method.invoke(Method.java:521)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-23 16:24:48.167: ERROR/AndroidRuntime(2530):     at dalvik.system.NativeStart.main(Native Method)

my code is mostly copied from the tutorial:

public class act_map extends MapActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        intent = getIntent();
        lat = intent.getDoubleExtra("lat", 0.0);
        lng = intent.getDoubleExtra("lng", 0.0);

        MapView = (MapView) findViewById(R.id.act_map_mapview);

        List<Overlay> mapOverlays = MapView.getOverlays();

        Drawable positionMarker 
            = getResources().getDrawable(R.drawable.act_map_androidmarker);
        MapItemizedOverlay itemizedOverlay
            = new MapItemizedOverlay(positionMarker);

        geoPoint = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
        Pointer = new OverlayItem(geoPoint, "Thats me!", "wohooo");

        itemizedOverlay.addOverlay(Pointer);

        mapOverlays.add(itemizedOverlay);

        MapView.setSatellite(false);

        MapController = MapView.getController();
        MapController.setCenter(geoPoint);
        MapController.setZoom(18);

        MapView.setBuiltInZoomControls(true);
        MapView.displayZoomControls(true);
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_I)
        {
            MapView.getController().setZoom(MapView.getZoomLevel() + 1);
            return true;
        }
        else if (keyCode == KeyEvent.KEYCODE_O)
        {
            MapView.getController().setZoom(MapView.getZoomLevel() - 1);
            return true;
        }
        else if (keyCode == KeyEvent.KEYCODE_S)
        {
            MapView.setSatellite(true);
            return true;
        }
        else if (keyCode == KeyEvent.KEYCODE_M)
        {
            MapView.setSatellite(false);
            return true;
        }

        return false;
    }

    public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem>
    {
        private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
        private Context mContext;

        public MapItemizedOverlay(Drawable defaultMarker)
        {
            super(boundCenterBottom(defaultMarker));
        }

        public MapItemizedOverlay(Drawable defaultMarker, Context context)
        {
            super(defaultMarker);
            mContext = context;
        }

        public void addOverlay(OverlayItem overlay)
        {
            mOverlays.add(overlay);
            populate();
        }

        @Override
        protected OverlayItem createItem(int i)
        {
            return mOverlays.get(i);
        }

        @Override
        public int size()
        {
            return mOverlays.size();
        }

        @Override
        protected boolean onTap(int index)
        {
            OverlayItem item = mOverlays.get(index);

            AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

            dialog.setTitle(item.getTitle());
            dialog.setMessage(item.getSnippet());
            dialog.show();

            return true;
        }
    }

How do I fix this?

like image 557
xenonite Avatar asked Jul 23 '10 14:07

xenonite


People also ask

Why does Google Maps crash on my phone?

Force Stop Google MapsIf the Google Maps app didn't start correctly or ran into a minor glitch, then it might cause the app to crash. In such cases, merely restarting the app should do the trick. Long-press on the Google Maps icon and select the App info. That'll take you to App Settings.

Why is Google Maps not working on Android Auto?

According to Liu, the issue is fixed by clearing Google Maps storage, then launching the Maps app on your phone, and starting Android Auto again. Users have already confirmed that this fix solves the problem.

What has happened to Google Maps?

The reports indicate that both the web and app versions of Google Maps are experiencing issues. Update 03/18/2022 4:20pm ET: Google confirmed that the technical issue has been fixed and that all Google Maps services are now back up globally.


1 Answers

I also had a problem with the Android MapView tutorial. When I clicked on the icon, the screen would crash (a null mContext would be passed):

The above fix did not work for me. Instead, I changed the following line of code from the MapActivity class from:

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

to this:

HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);

You also need to fix constructor by adding boundCenterBottom which is missing:

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

(Had beautiful screenshots but can't post because I am new :P)

Hope that helps others who are still having a problem with the tutorial and fixes their apps!

like image 98
hasanzuav Avatar answered Nov 15 '22 04:11

hasanzuav