2012-06-04 64 views
0

在谷歌地圖上的標記存在onTap問題。onTap在地圖標記上給我NullPointerException

這是錯誤

 
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; 
     } 

線refered在錯誤日誌是這樣的

AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

什麼,我可以從我的谷歌搜索一下猜測,它可能是mContext沒有傳承下去......但我不能得到它的權利......

請幫

+0

你是如何構建LocationOverlay一個子類? –

+0

不知道你想知道什麼:) – Jerry

+0

當您調用構造函數LocationOverlay(mapView,defaultMarker,context)時發佈代碼 –

回答

1

檢查您的來電構造函數的空值可能會被傳遞到那裏。您可以使用this關鍵字,因爲ActivityContex噸像這樣

LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), this); 

或者,如果你是從一個片段調用使用getActivity()方法

LocationOverlay locationOverlay = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.polis), this.getActivity());