为什么我的程序崩溃了?

发布于 2024-10-19 11:41:33 字数 4310 浏览 3 评论 0原文

您好,我正在使用本指南创建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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2024-10-26 11:41:33

似乎您调用了错误的构造函数,未传递上下文。

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

Seems like you call the wrong constructor that doesn't pass a Context.

final MapItem itemizedStoreOverlay = new MapItem(storePic); // No context.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文