views:

69

answers:

1

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?

+1  A: 

mContext is null. Since your overlay is an inner class of the activity, replace mContext with act_map.this in your AlertDialog constructor call.

CommonsWare
thanx! this solved the problem!
xenonite