Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:00:37+00:00 2026-05-24T09:00:37+00:00

i am using MapActivity in my application to show map and for that i

  • 0

i am using MapActivity in my application to show map and for that i have a class named MapItemizedOverlay which extends ItemizedOverlay. I added an AlertDialog on onTap() function of MapItemizedOverlay class. After running the application, when i click on the marker of a point the application crashed giving a BadTokenException. I tried to debug using Log and it shows me the problem is on dialog.show() line where dialog is my AlertDialog’s object. Now, i don’t understand whats the exact problem with the code is ? :-/ I am giving my code below, Please guys help me on the issue …. Thanks in advance ….. !!!

MapActivity Class :

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

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;

public class VisIKart extends MapActivity {

Animation slide;

MapView mapView;
MapController myMapController;
List<Overlay> mapOverlays;
MapItemizedOverlay itemizedoverlay;

private double current_longitude;
private double current_latitude;

protected LocationManager locationManager;

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.visikart);

    TextView txtbtn = (TextView) findViewById(R.id.vikinfotxt);
    slide = AnimationUtils.loadAnimation(this, R.anim.right_slide_in);
    txtbtn.startAnimation(slide);
    TextView txtinfo = (TextView) findViewById(R.id.vik);
    slide = AnimationUtils.loadAnimation(this, R.anim.right_slide_in);
    txtinfo.startAnimation(slide);

    Button bckbtn = (Button) findViewById(R.id.vikinfo);
    bckbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Information.animflaginfo = 1;
            Intent myIntent = new Intent(v.getContext(), Information.class);
            Activity1.group.replaceContentView("Information", myIntent);

        }
    });

    getUserCurrentLocation();

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

    mapOverlays = mapView.getOverlays();

    Drawable drawable = this.getResources().getDrawable(R.drawable.barpin);
    itemizedoverlay = new MapItemizedOverlay(drawable,this);

    GeoPoint point = new GeoPoint((int)(Information.lattitude* 1e6),(int)(Information.longitude* 1e6));

    mapView.setFocusable(true);
    myMapController = mapView.getController();
    myMapController.animateTo(point);
    myMapController.setZoom(13);
    myMapController.setCenter(point);

    OverlayItem overlayitem = new OverlayItem(point, "Bar Name", "");

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

    mapView.setStreetView(true);

    Button userloc = (Button) findViewById(R.id.vikuser);
    userloc.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            if(!mapView.getOverlays().isEmpty()) 
            { 
                mapOverlays.clear();
                itemizedoverlay.mOverlays.clear();
                mapView.invalidate();

            }

            Drawable drawable1 = VisIKart.this.getResources().getDrawable(R.drawable.userpin);
            itemizedoverlay = new MapItemizedOverlay(drawable1,VisIKart.this);

            GeoPoint point = new GeoPoint((int)(current_latitude * 1e6),(int)(current_longitude * 1e6));

            mapView.setFocusable(true);
            myMapController = mapView.getController();
            myMapController.animateTo(point);
            myMapController.setZoom(13);
            myMapController.setCenter(point);

            OverlayItem overlayitem = new OverlayItem(point, "User Location", "");

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

            mapView.setStreetView(true);

        }
    });

}

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {

    }

    public void onStatusChanged(String s, int i, Bundle b) {

    }

    public void onProviderDisabled(String s) {

    }

    public void onProviderEnabled(String s) {

    }
}

private void getUserCurrentLocation() { 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    String provider = locationManager.getBestProvider( new Criteria(), true );
    if ( provider != null )
    {
        System.out.println( "Using provider: " + provider );
        locationManager.requestLocationUpdates( provider, 120 * 1000, 10000, new MyLocationListener() );
    }

    Location lastLocation = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
    if ( lastLocation != null )
        useLocation( lastLocation );
    else
        useLocation( locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER ) );
}

private void useLocation(Location user_location) {
    if(user_location != null) {
        current_longitude = user_location.getLongitude();
        current_latitude = user_location.getLatitude();

    }

}

}

MapItemizedOverlay Class:

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.util.Log;

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

public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem> {

protected ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

Context mContext;

AlertDialog.Builder dialog;
    AlertDialog alert;

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

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlays.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mOverlays.size();

}

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

  //Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_LONG).show();
  /*Dialog dialog = new Dialog(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setCancelable(true);
  dialog.show();*/

  dialog = new AlertDialog.Builder(mContext);
  Log.v("After","Defining");
  dialog.setTitle(item.getTitle());
  Log.v("After","setTitle");
  dialog.setMessage(item.getSnippet());
  Log.v("After","setMessage");
  dialog.setCancelable(true);
  Log.v("After","setCancelable");
  dialog.setPositiveButton(null, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
          /*Log.i(this.getClass().getName(), "Selected Yes To Add Location");
          ((VisIKart) mContext).finish();*/
      }
  });
  Log.v("After","setPositiveButton");
  dialog.setNegativeButton(null, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
          /*Log.i(this.getClass().getName(), "Selected No To Add Location");
          dialog.cancel();*/
      }
  });
  Log.v("After","setNegativeButton");
  alert = dialog.create();
  Log.v("After","create");
  alert.show();
  Log.v("After","show");
  return true;
}

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:00:38+00:00Added an answer on May 24, 2026 at 9:00 am

    Its solved , I have to use getParent() in the expression given below,

    itemizedoverlay = new MapItemizedOverlay(drawable,getParent());
    

    As my Activity extends TabActivity ….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an application that creates notifications, using the following code: // notification
Friends i am making an application in which i am using Map Activity. Now
I have the following code in which I am using the application context to
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active
I have developed some reusable android component which is basically a class . This
My application consists of a MapActivity , which loads an XML view (map_layout), containing
I have an activity(say, MyActivity) that contains fragments, and i am using fragmentTransaction.replace(XX,YY) for
i have made an application in which i need to load the google maps.
I'm using IntelliJ IDEA 9 (9.0.1) to create a simple map application (or at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.