android - java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()”

问题描述 投票:0回答:0

我收到 getMyLocation() 函数的错误

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

// current_location_latitude = 32.361114 ; // current_location_longitutde = 74.207883 ;

    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    getMyLocation();
    location = new LatLng(current_location_latitude, current_location_longitutde);
    mMap.addMarker(new MarkerOptions().position(location).title("Your Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
    mMap.animateCamera( CameraUpdateFactory.zoomTo( 16.0f ) );  // zoom in
}

public  void getMyLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
    } else {
        lm = (LocationManager)  this.getSystemService(Context.LOCATION_SERVICE);
        Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        current_location_latitude = loc.getLatitude();
        current_location_longitutde = loc.getLongitude();
        // Toast.makeText(getApplicationContext(),current_location_latitude+" , "+ current_location_longitutde , Toast.LENGTH_SHORT).show();
    }
}

我尝试更改 API 密钥,尝试了 mMap.setMyLocationEnabled(true) 但都没有用。

android
© www.soinside.com 2019 - 2024. All rights reserved.