强制关闭在Android的空指针异常 [英] Force Closed with Null Pointer Exception in Android

查看:191
本文介绍了强制关闭在Android的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种低于code这我试图测试出在模拟器上先只是为了确保,它工作正常,那么我就可以开始测试其真实的设备。

I have this below code which I am trying to test out on the Emulator firstly just to make sure, it is working fine, then I can start testing it on real device.

下面code创建谷歌地图在下半部分的Andr​​oid屏幕和上半部的TextView 。据我所知,只要你开始有谷歌地图应用程序,你需要从DDMS角度传的经度和纬度坐标。

Below code creates a Google Map on the top half of the Android screen and in the Bottom Half the TextView. As far as I know whenever you start your application having Google Map, you need to pass the latitude and longitude co-ordinates from the DDMS perspective.

但在我而言,我不传递任何位置坐标,这下面的程序是扔空指针异常在该行 -

But in my case, I am not passing any location co-ordinates and this below program is throwing NULL POINTER EXCEPTION at this line-

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

我不知道为什么会发生,据我所知,谷歌地图应该得到先加载,那么它应该等待通过的位置,距离坐标DDMS透视图,但只要我启动我的申请,我得到强制关闭 NPE

I am not sure why is it happening, As far as I know, Google Map should get loaded firstly, and then it should wait to pass the location coordinates from the DDMS perspective, but as soon as I launch my application, I get force closed with the NPE.

任何思考为什么会发生?

Any thoughts why is it happening?

以下是完整code -

Below is the full code-

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

位置更新类 -

Location Update class-

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

更新: -

我发现这个问题,我想,当我的叠加添加到列表中,它总是会立即开始绘制。不要紧,我是否没有得到一个位置。 我怎么能有一个安全,高效的方式来添加,直到有设置位置不添加覆盖?

I found the problem, I guess when I add the overlay to the list, it will always start drawing immediately. It doesn't matter whether or not I get a location. How can I have a safe and efficient way to add the to not add the overlay until there is a location to set.?

推荐答案

安全方式:

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

这篇关于强制关闭在Android的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆