我在我的应用程序中使用MapActivity来显示地图,为此我有一个名为MapItemizedOverlay的类,它扩展了ItemizedOverlay。我在MapItemizedOverlay类的onTap()函数中添加了一个AlertDialog。运行应用程序后,当我单击某个点的标记时,应用程序崩溃并发出BadTokenException。我尝试使用Log进行调试,它向我显示问题出在dialog.show()行,其中对话框是我的AlertDialog的对象。现在,我不明白代码的确切问题是什么? : - /我在下面给我的代码,请各位帮我解决这个问题....提前致谢..... !!!
MapActivity类:
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类:
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();
}
}
答案 0 :(得分:0)
它解决了,我必须在下面给出的表达式中使用getParent(),
itemizedoverlay = new MapItemizedOverlay(drawable,getParent());
由于我的活动扩展了TabActivity ....