0

Making a simple map app, plan on adding buttons to mark specific locations

But right now it crashes when I zoom.

import java.util.List;

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


import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class Main extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable);

    GeoPoint point = new GeoPoint(19240000,-99120000);
    OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");

    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);
    }



@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
   }

When I touch the built in zoom, it forces the application to close.

Any help would be great.

edit: Stacktrace

12-16 05:40:04.012: D/dalvikvm(2473): GC_FOR_MALLOC freed 4578 objects / 277208 bytes in 49ms
12-16 05:40:04.151: I/MapActivity(2473): Handling network change notification:CONNECTED
12-16 05:40:04.151: E/MapActivity(2473): Couldn't get connection factory client
12-16 05:40:04.271: D/dalvikvm(2473): GC_FOR_MALLOC freed 3049 objects / 271640 bytes in 49ms
12-16 05:40:08.491: D/AndroidRuntime(2473): Shutting down VM
12-16 05:40:08.491: W/dalvikvm(2473): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-16 05:40:08.551: E/AndroidRuntime(2473): FATAL EXCEPTION: main
12-16 05:40:08.551: E/AndroidRuntime(2473): java.lang.NullPointerException
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.b00517566.la.MyItemizedOverlay.onTap(MyItemizedOverlay.java:46)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:647)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.view.View.dispatchTouchEvent(View.java:3766)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)

12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)

12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.os.Looper.loop(Looper.java:123)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at java.lang.reflect.Method.invokeNative(Native Method)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at java.lang.reflect.Method.invoke(Method.java:521)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-16 05:40:08.551: E/AndroidRuntime(2473):     at dalvik.system.NativeStart.main(Native Method)
12-16 05:40:11.023: I/Process(2473): Sending signal. PID: 2473 SIG: 9

EDIT

    package com.bleh.la;

    import java.util.ArrayList;

    import android.app.AlertDialog;
    import android.content.Context;
    import android.graphics.drawable.Drawable;

    import com.google.android.maps.ItemizedOverlay;
    import com.google.android.maps.OverlayItem;

     public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {


private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;


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

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();
}

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

@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;
}
    }
5
  • just cleared the logs for a fresh run stack, now it works just fine...fml. don't suppose you could give me some pointers for having buttons make the map pointer hover over a differnt location? or should I make a new question? Dec 16, 2011 at 5:37
  • scratch that, its doing it again. Worked once, but now its crashing every time. standby for stacktrace Dec 16, 2011 at 5:39
  • 1
    It seems that there is some problem in the MyItemizedOverlay. It would be nice to see its sources to find out the reason to crash. Dec 16, 2011 at 5:51
  • by sources I assumed you meant source code? which is above Dec 16, 2011 at 6:00
  • I edited my answer with link just go through it..
    – user370305
    Dec 16, 2011 at 6:28

2 Answers 2

1

I think , this line give you null pointer exception

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

Here mContext is null

because of In your Main.java you are calling,

MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable);

instead of call

  MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable,this);

And your constructor should be like,

public MyItemizedOverlay(Drawable drawable,Context cxt){
        super(boundCenter(drawable));
         this.mContext = cxt;
    }

EDIT: Look at the Sample Project

Using Google Maps in Android

2
  • Adding ",this" to the MyItemizedOverlay, causes teh marker to disappear Dec 16, 2011 at 6:21
  • I changed teh second "super" to this(defaultMarker) instead of super(defaultMarker). so far its stable, Im gonna debug some more Dec 16, 2011 at 6:27
1

Your MyItemizedOverlay.mContext is null and this causes NPE. I suggest you to remove constructor that takes only drawable and change the creation of the overlay in the activity into

MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);

There is also another difference in constructors - omitted call to boundCenterBottom in the second one - is this intentional?:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.