谷歌地图中的标记上有onTap问题。
这是错误
05-31 21:46:21.420: E/AndroidRuntime(5541): java.lang.NullPointerException 05-31 21:46:21.420: E/AndroidRuntime(5541): at com.android.internal.app.AlertController$AlertParams.(AlertController.java:753) 05-31 21:46:21.420: E/AndroidRuntime(5541): at android.app.AlertDialog$Builder.(AlertDialog.java:273) 05-31 21:46:21.420: E/AndroidRuntime(5541): at my.class.HelloMapView$LocationOverlay.onTap(HelloMapView.java:1361)
这是my.class.HelloMapView错误的代码$ LocationOverlay.onTap(HelloMapView.java:1361)
public class LocationOverlay extends ItemizedOverlay<OverlayItem> {
//public class LocationOverlay extends ItemizedOverlay{
private ArrayList<OverlayItem> overLayList = new ArrayList<OverlayItem>();
private MapView mapView;
public String pickedlat;
public String pickedlng;
private Context mContext;
public LocationOverlay(MapView mapView, Drawable defaultMarker, Context context) {
//super(null);
super(boundCenterBottom(defaultMarker));
mContext = context;
this.mapView = mapView; // need it for onTap
populate();
}
@Override
protected OverlayItem createItem(int i) {
return overLayList.get(i);
}
@Override
public int size() {
return overLayList.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
if(!overLayList.contains(overlayItem)){
overLayList.add(overlayItem);
setLastFocusedIndex(-1);
populate();
}
// populate();
}
@Override
protected boolean onTap(int pIndex)
{
OverlayItem item = overLayList.get(pIndex);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
错误日志中引用的行是
AlertDialog.Builder对话框=新的AlertDialog.Builder(mContext);
我可以通过谷歌搜索猜测它,可能是mContext没有通过...但我不能正确...
请帮忙
答案 0 :(得分:1)
检查对构造函数的调用,可能会在那里传递null值。您可以使用this
关键字,因为Activity
是Contex
的子类,所以
LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), this);
或者如果您从Fragment调用,请使用getActivity()方法
LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), this.getActivity());