1

我有下面的代码,我首先尝试在模拟器上进行测试,以确保它工作正常,然后我可以开始在真实设备上测试它。

下面的代码Google Map在 Android 屏幕的上半部分和下半部分创建TextView. 据我所知,每当您启动具有 Google Map 的应用程序时,您都需要从 DDMS 角度传递纬度和经度坐标。

但就我而言,我没有传递任何位置坐标,下面的程序正在抛出NULL POINTER EXCEPTION这一行 -

mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);

我不确定为什么会这样,据我所知,Google Map应该首先加载,然后它应该等待从 DDMS 角度传递位置坐标,但是一旦我启动我的应用程序,我就会force closed得到NPE.

任何想法为什么会发生?

以下是完整代码-

private MapView mapView;
private ListView listView;

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

    mapView = (MapView) findViewById(R.id.mapView);
    listView = (ListView) findViewById(R.id.mylist);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener(mapView);

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            35000, 
            0, 
            locationListener);


    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);
    mapController = mapView.getController();
    mapController.setZoom(15);
}

位置更新类-

    private class GPSLocationListener implements LocationListener {

    MapOverlay mapOverlay;

    public GPSLocationListener(MapView mapView) {
        mapOverlay = new MapOverlay(this,android.R.drawable.star_on);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.add(mapOverlay);
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));

            mapController.animateTo(point);
            mapController.setZoom(15);

            mapOverlay.setPointToDraw(point);
            mapView.invalidate();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

下面是在当前位置的地图上绘制一个圆圈的类,而NPE 仅在该类中发生-

    class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames=new int[6];
    private Point mScreenPoints;
    private Bitmap mBitmap;
    private Paint mCirclePaint;


    public MapOverlay(GPSLocationListener gpsLocationListener, int currentUser) {
        imageNames[0]=currentUser;
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(0x30000000);
        mCirclePaint.setStyle(Style.FILL_AND_STROKE);
        mBitmap = BitmapFactory.decodeResource(getResources(),imageNames[0]);
        mScreenPoints = new Point();
    }

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);

                   // NPE happening here
        mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);

        int totalCircle=4;
        int radius=40;
        int centerimagesize=35;

        for (int i = 1; i <= totalCircle; i ++) { 
            canvas.drawCircle(mScreenPoints.x,mScreenPoints.y, i*radius, mCirclePaint); 
        } 

        canvas.drawBitmap(mBitmap, (mScreenPoints.x-(centerimagesize/2)),(mScreenPoints.y-(centerimagesize/2)), null);
        super.draw(canvas,mapView,shadow);

        return true;
    }
}

更新:-

我发现了问题,我猜当我将叠加层添加到列表时,它总是会立即开始绘制。我是否得到一个位置并不重要。我怎样才能有一种安全有效的方法来添加不添加覆盖,直到有一个位置来设置。?

4

3 回答 3

1
mapView = (MapView) findViewById(R.id.mapView);

它将创建地图视图并开始加载地图。

locationManager.requestLocationUpdates

您正在请求位置,但它会稍后到达,谁知道何时,也许 50 秒后!

public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));

            mapController.animateTo(point);
            mapController.setZoom(15);

            mapOverlay.setPointToDraw(point);
            mapView.invalidate();
        }
    }

只有在这里你设置点,什么时候到达位置

mapOverlay.setPointToDraw(point);

但是地图已经显示并且 draw() 已被调用。

于 2012-09-17T03:40:08.513 回答
1

安全方式:

public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
    if(pointToDraw == null){ // it isn't found the location yet.
         return super.draw(canvas, mapView, shadow); // do the default
    }        

   // else:
    super.draw(canvas, mapView, shadow);

                   // NPE happening here
        mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);

        int totalCircle=4;
        int radius=40;
        int centerimagesize=35;

        for (int i = 1; i <= totalCircle; i ++) { 
            canvas.drawCircle(mScreenPoints.x,mScreenPoints.y, i*radius, mCirclePaint); 
        } 

        canvas.drawBitmap(mBitmap, (mScreenPoints.x-(centerimagesize/2)),(mScreenPoints.y-(centerimagesize/2)), null);
        super.draw(canvas,mapView,shadow);

        return true;
    }
于 2012-09-17T08:45:56.830 回答
0
if(mapView != null){
    Projection projection = mapView.getProjection();
    if(projection != null){
          mScreenPoints = projection.toPixels(pointToDraw, mScreenPoints);
    }
    else{
      // log it projection is null
    }
}
else{
   //log it mapView is null
}

现在空指针在哪里?

于 2012-09-17T03:23:17.020 回答