我有以下代码,我试图在模拟器上测试首先只是为了确保它工作正常,然后我可以开始在真实设备上测试它。
下面的代码会在Android屏幕的上半部分和Google Map
的下半部分创建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;
}
}
更新: -
我发现了问题,我想当我将叠加层添加到列表中时,它将始终立即开始绘制。我是否得到一个位置并不重要。 如果要设置位置,我怎样才能安全有效地添加不添加叠加层?
答案 0 :(得分: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()。
答案 1 :(得分: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;
}
答案 2 :(得分: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
}
现在nulllpointer在哪里?