Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中强制关闭空指针异常_Java_Android - Fatal编程技术网

Java Android中强制关闭空指针异常

Java Android中强制关闭空指针异常,java,android,Java,Android,我有下面的代码,我正试图首先在模拟器上测试,以确保它工作正常,然后我可以开始在真实设备上测试它 下面的代码在Android屏幕的上半部分和下半部分创建了一个谷歌地图。据我所知,无论何时使用GoogleMap启动应用程序,都需要从DDMS角度传递纬度和经度坐标 但在我的例子中,我没有传递任何位置坐标,下面的程序在这一行抛出空指针异常- mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints); 我不知

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

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

但在我的例子中,我没有传递任何位置坐标,下面的程序在这一行抛出
空指针异常-

mScreenPoints = mapView.getProjection().toPixels(pointToDraw, mScreenPoints);
我不知道为什么会发生这种情况,据我所知,
googlemap
应该先加载,然后等待从DDMS角度传递位置坐标,但一旦启动应用程序,我就会使用
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) {
    }
}
私有类GPSLocationListener实现LocationListener{
地图覆盖;
公共GPSLocationListener(MapView MapView){
mapOverlay=新的mapOverlay(这个,android.R.drawable.star\u打开);
List ListoForLays=mapView.getOverlays();
添加(映射覆盖);
}
@凌驾
已更改位置上的公共无效(位置){
如果(位置!=null){
地质点=新的地质点(
(int)(location.getLatitude()*1E6),
(int)(location.getLongitude()*1E6));
mapController.animateTo(点);
mapController.setZoom(15);
mapOverlay.setPointToDraw(点);
mapView.invalidate();
}
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
}
下面是在地图上当前位置画一个圆圈的班级,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;
    }
}
class MapOverlay扩展了覆盖{
私人地质点绘制;
int[]imageNames=新的int[6];
私用点;
私有位图mBitmap;
私人油漆;
公共映射覆盖(GPSLocationListener GPSLocationListener,int currentUser){
imageNames[0]=当前用户;
mCirclePaint=新油漆(油漆.防油漆别名标志);
mCirclePaint.setColor(0x30000000);
mCirclePaint.setStyle(样式填充和笔划);
mBitmap=BitmapFactory.decodeResource(getResources(),imageNames[0]);
mScreenPoints=新点();
}
公共无效设置点ToDraw(地质点){
pointToDraw=点;
}
公共地质点getPointToDraw(){
返回点ToDraw;
}
公共布尔绘制(画布画布、地图视图、地图视图、布尔阴影、长时间){
super.draw(画布、地图视图、阴影);
//NPE发生在这里
mScreenPoints=mapView.getProjection().toPixels(pointToDraw,mScreenPoints);
int totalCircle=4;
int半径=40;
int centerimagesize=35;
对于(int i=1;i
现在空指针在哪里

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()已经被调用。

安全方式:

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;
    }
public boolean draw(画布、地图视图、地图视图、布尔阴影、长时间){
如果(pointToDraw==null){//尚未找到该位置。
返回super.draw(画布、地图视图、阴影);//执行默认设置
}        
//其他:
super.draw(画布、地图视图、阴影);
//NPE发生在这里
mScreenPoints=mapView.getProjection().toPixels(pointToDraw,mScreenPoints);
int totalCircle=4;
int半径=40;
int centerimagesize=35;

for(int i=1;i mapView.getProjection()为空?因为您没有等待地图加载完成?您是否调试它以确定您从何处获得NPE…mapView为空还是mapVie.getProjection()为空返回null,pointsToDraw或mScreenPoints是否为null?@matheszabi,这可能是可能的,那么我如何才能等待映射加载完成?我做错了什么?或者可能没有使用setter,pointToDraw为null?尝试打断该语句,查看其中的null,并插入if(myVariable!=null){doSomething();}Block但我的问题是为什么它会直接进入该方法,因为我没有从DDMS透视图传递任何坐标。我在等待我的地图首先被加载,然后我将从DDMS透视图传递坐标。好的。我想找到了。
pointToDraw为Null
但是为什么?非常奇怪?而且我也很奇怪-为什么我t将直接进入该方法,因为我还没有从DDMS透视图传递任何坐标。我正在等待我的地图首先被加载,然后我将从DDMS透视图传递坐标。但如果没有首先加载地图,它会认为我已经传递了坐标,但我没有传递任何坐标和我想这就是为什么它是空的。对我来说不是,因为唯一不为空的方法是公共的void setPointToDraw(GeoPoint point){pointToDraw=point;},在公共布尔draw()之前没有调用它方法然后是什么问题?我希望,我很清楚我想从我的应用程序中得到什么。我只需要每隔30秒在我的当前位置上画一个圆圈。首先我会使用真实的设备,特别是GPS、视频、音频,其次:我会将pointToDraw变量初始化为任何值。我这是你的首选
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;
    }