0

在谷歌地图中的标记上的 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): 在 android.app.AlertDialog$Builder.(AlertDialog.java:273)
05-31 21:46:21.420: E/AndroidRuntime(5541): 在 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 对话框 = new AlertDialog.Builder(mContext);

我可以从我的谷歌搜索中猜到,它可能是 mContext 没有传递......但我无法正确理解......

请帮忙

4

1 回答 1

1

检查您对构造函数的调用,可能会在其中传递空值。您可以使用this关键字,因为Activity它是Context 的子类,就像这样

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());
于 2012-06-06T01:21:15.813 回答