开发者

AlertDialog Show Problem In A Class Which Extends ItemizedOverlay<OverlayItem>

开发者 https://www.devze.com 2023-03-26 13:25 出处:网络
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 MapItemizedOverla

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

privat开发者_高级运维e 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();
}

}


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

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

As my Activity extends TabActivity ....

0

精彩评论

暂无评论...
验证码 换一张
取 消