为什么我的程序崩溃了?
您好,我正在使用本指南创建mapView和mapOverlays: http:// /developer.android.com/resources/tutorials/views/hello-mapview.html
这是我的代码:
my 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;
}
}
my mapview class (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();
}
}
问题是,当我按在商店标记上,我的程序因以下错误而崩溃: http://elysobi.kjm.sgsnet.se:81/public/Android /ERROR.png
其中mapItem 中的第50 行在我的mapoverlay 上被标记。
有人知道问题是什么吗?
/感谢您的帮助,本。
Hello I'm using this guide to create a mapView and mapOverlays: http://developer.android.com/resources/tutorials/views/hello-mapview.html
and here is my code:
my 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;
}
}
my mapview class (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();
}
}
The problem is that when I press on a storemarker my program crashes with this error:
http://elysobi.kjm.sgsnet.se:81/public/Android/ERROR.png
where row 50 in mapItem is marked on my mapoverlay.
Anyone knows whats the problem is?
/Thankful for help, Ben.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您调用了错误的构造函数,未传递上下文。
Seems like you call the wrong constructor that doesn't pass a Context.