AlertDialog 显示扩展 ItemizedOverlay的类中的问题
我在应用程序中使用 MapActivity 来显示地图,为此我有一个名为 MapItemizedOverlay 的类,它扩展了 ItemizedOverlay。我在 MapItemizedOverlay 类的 onTap() 函数上添加了 AlertDialog。运行应用程序后,当我单击某个点的标记时,应用程序崩溃并给出 BadTokenException。我尝试使用 Log 进行调试,它显示问题出在dialog.show() 行,其中dialog 是我的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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它解决了,我必须在下面给出的表达式中使用 getParent() ,
因为我的 Activity 扩展了 TabActivity ....
Its solved , I have to use getParent() in the expression given below,
As my Activity extends TabActivity ....