【问题标题】:onTap on map-marker give me NullPointerException地图标记上的 onTap 给我 NullPointerException
【发布时间】:2012-06-04 22:26:40
【问题描述】:

在谷歌地图上的标记上的 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 没有传递...但我无法正确...

请帮忙

【问题讨论】:

  • 你是如何构建 LocationOverlay 的?
  • 不知道你现在想知道什么 :)
  • 在调用构造函数LocationOverlay(mapView, defaultMarker, context)时贴出代码
  • LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), mContext);
  • 确保 mContext 不为空。如果您从 Activity 调用,只需使用此关键字,如下所示 LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), this);

标签: android google-maps


【解决方案1】:

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

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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 2021-03-18
    • 1970-01-01
    • 2021-03-09
    • 2021-11-13
    相关资源
    最近更新 更多