Finish Activity - Google Maps Android

1.1k Views Asked by At

I'm doing a project with Google Maps for Android. I've created a MapActivity to load some places I've saved on the server. So I have this class

public class PlaceItemizedOverlay extends ItemizedOverlay<OverlayItem> {

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

    public PlaceItemizedOverlay(Drawable defaultMarker, MapActivity map) {
          super(boundCenterBottom(defaultMarker));
              this.macAct = map;
    }

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

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

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


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

     this.mapAct.finish();

      return true;
    }

Well, when I click at a saved place, the program breaks.

I'm creating the object on the mapActivity:

PlaceItemzedOverlay itemizedoverlay = new PlaceItemizedOverlay(drawable, this);

Can you help me please?

Many Thanks!

2

There are 2 best solutions below

1
On BEST ANSWER

the problem is here //part of your code

  @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();//here you show a alert dialog on current activity 

  //this.mapAct.finish();//and here you finish current activity

  return true;
}

at this situation application crashes.. and use

finish(); 

for your

 this.mapAct.finish();
0
On

If, as you say, you are creating your itemizedoverlay object using...

PlaceItemzedOverlay itemizedoverlay = new PlaceItemizedOverlay(drawable, this);

...then the following line isn't valid...

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

Basically you have two constuctors for PlaceItemzedOverlay. The second parameter of one is a Context and the other is a MapActivity. These are the only two places where either mContext or mapAct are set to a valid reference and both are mutually exclusive. In other words whichever constructor is used, one or other of mContext or mapAct will remain null.

Mixing your use of mContext and mapAct in your onTap method is never going to work...

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

...

this.mapAct.finish();