向double数组添加double值时出错

时间:2014-04-03 17:05:48

标签: java android nullpointerexception double locationmanager

我正在尝试将位置值添加到双数组中,但它给了我nullpointerexcetion。我要求像这样的位置更新

if (buttonView.isChecked()) {
                        if (!manager
                                .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                            mylocation = manager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            location_checked = true;
                        } else {
                            manager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER, 5000, 0,
                                    listener);
                            location_checked = true;
                        }
                    }
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                       5000,0, listener);

然后我将传入的位置值设置为myLocation

LocationListener listener = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        mylocation = location;

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

};

然后我尝试将值添加到双数组但是它给了我错误并在此时崩溃了我的应用

double[] locationData = new double[] {mylocation.getLatitude(),  
                                      mylocation.getLongitude() };

1 个答案:

答案 0 :(得分:0)

你应该这样:

    @Override
  public void onLocationChanged(Location location) {
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    setlocationData(lat, lng);
  }

因为直到LocationChanged设置了Location,它才为null。

在LocationManager获取de GPS信号之前需要一些时间。