2011-02-26 191 views
0

你好,我用這個指南來創建一個MapView和mapOverlays:http://developer.android.com/resources/tutorials/views/hello-mapview.html爲什麼我的程序崩潰?

這裏是我的代碼:

我mapoverlay(mapItem):

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 
private Context mContext; 

public MapItem(Drawable defaultMarker) { 
    super(boundCenterBottom(defaultMarker)); 

} 

public MapItem(Drawable defaultMarker, Context context) { 
    super(boundCenterBottom(defaultMarker)); 
    mContext = context; 
} 

public void addOverlay(OverlayItem overlay) { 
    mOverlays.add(overlay); 
    populate(); 
} 

@Override 
protected OverlayItem createItem(int i) { 
    return mOverlays.get(i); 
} 

@Override 
public int size() { 
    return mOverlays.size(); 
} 

//remove oldest element 
public void remove(){ 
     mOverlays.remove(0); 
} 

@Override 
protected boolean onTap(int index) { 
    OverlayItem item = mOverlays.get(index); 
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); //row 50 
    dialog.setTitle(item.getTitle()); 
    dialog.setMessage(item.getSnippet()); 
    dialog.show(); 
    return true; 
} 

}

我MapView類類(mapViewActivity):

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mapview); 
    setupListener(); 

    final MapView mapView = (MapView) findViewById(R.id.mapview); 
    mapController = mapView.getController(); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setClickable(true); 

    //Store resources 
    final List<Overlay> storeOverlays = mapView.getOverlays(); 
    Drawable storePic = this.getResources().getDrawable(R.drawable.storemarker); 
    final MapItem itemizedStoreOverlay = new MapItem(storePic); 

    mapController.setZoom(13); // defualt zoom 

    //Plot all stores on the map 
    df = ((ClassHandler)getApplication()).getDatabaseFacade(); 

    Cursor plotstore = df.getAllStorePos("Göteborg"); 
    startManagingCursor(plotstore); 
    plotstore.moveToFirst(); 

    while(plotstore.isAfterLast() == false){ 

     GeoPoint addStore = new GeoPoint(plotstore.getInt(plotstore.getColumnIndex("lat")), plotstore.getInt(plotstore.getColumnIndex("long"))); 
     OverlayItem overlayitem = new OverlayItem(addStore, plotstore.getString(plotstore.getColumnIndex("_id")), plotstore.getString(plotstore.getColumnIndex("address"))); 

     itemizedStoreOverlay.addOverlay(overlayitem); 
     storeOverlays.add(itemizedStoreOverlay); 
     plotstore.moveToNext(); 
    } 

     //Get current position 
     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     LocationListener locationListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
       GeoPoint currentPos = new GeoPoint((int)(location.getLatitude()*1000000), (int)(location.getLongitude()*1000000)); 
       mapController.animateTo(currentPos); 
     }    


     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, 
       Bundle extras) { 
      // TODO Auto-generated method stub 

     } 
    }; 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

    //add current position 
    MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 
    myLocationOverlay.enableMyLocation(); 

    //myLocationOverlay.disableMyLocation(); 
} 

private void setupListener() { 

    MapListener listener = new MapListener(); 
} 

}

的問題是,當我按下一個storemarker我的程序與此錯誤崩潰: http://elysobi.kjm.sgsnet.se:81/public/Android/ERROR.png

其中mapItem行50標在我的mapoverlay。

有誰知道什麼問題?

/感謝幫助,本。

+0

1)你確定mContext被初始化了嗎? 2)你有2個版本的MapItem,第一個沒有初始化mContext – fazo 2011-02-26 13:21:24

回答

1

好像你調用錯誤的構造函數,不傳遞的上下文。

final MapItem itemizedStoreOverlay = new MapItem(storePic); // No context. 
+0

final MapItem itemizedStoreOverlay = new MapItem(storePic,this); – 2011-02-26 13:28:36

相關問題