我有一个扩展MapActivity的主类,我将在子类中创建警告对话框,但是我遇到了Context参数的错误。 这是我的java代码:
public class main扩展了MapActivity {
private MapView mapView;
private MapController mapController;
private GeoPoint geopoint;
public class MapOverlay extends Overlay {
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapv){
if(event.getAction()==1){
GeoPoint p = mapv.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(), "Location: " +
p.getLatitudeE6()/1E6 + "," +
p.getLongitudeE6()/1E6, Toast.LENGTH_LONG).show();
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage("Hello World");
ad.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
}
return false;
}
}
}
错误是:
构造函数AlertDialog.Builder(main.MapOverlay)未定义!
我将其更改为getBaseContext()
,但我遇到了Force Close错误。
答案 0 :(得分:0)
您可以使用继承的getContext()
方法从MapView
引用中获取对有效上下文的引用。相关的行将是:
AlertDialog ad = new AlertDialog.Builder(mapv.getContext()).create();