gpt4 book ai didi

java - 在 Android 中使用空指针异常强制关闭

转载 作者:行者123 更新时间:2023-11-29 21:58:52 26 4
gpt4 key购买 nike

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

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

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

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

我不确定为什么会这样,据我所知,Google Map 应该首先加载,然后它应该等待从 DDMS 角度传递位置坐标,但尽快当我启动我的应用程序时,我得到了 NPEforce closed

知道为什么会这样吗?

下面是完整的代码-

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) {
}
}

下面是在当前位置的 map 上绘制一个圆的类,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;
}
}

更新:-

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

最佳答案

安全方法:

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;
}

关于java - 在 Android 中使用空指针异常强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12452663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com