0

I'm making app with Google Map but i have a problem with permissions. Someone can help me please? While Android 10 has no problem, Android 9 has this problem. These 106-115 row's codes

if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            requestPermissions(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),1)

        }else{
            locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER,2,2f,locationListener)
            mMap.clear()
            var lastLocation = locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
            var lastUserLocation = LatLng(lastLocation!!.latitude,lastLocation!!.longitude)
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastUserLocation,17f))
        }

enter image description here

cihatid
  • 19
  • 2
  • 5
  • 3
    Since `getLastKnownLocation()` frequently returns `null`, using `!!` is not a good idea. – CommonsWare May 23 '20 at 23:56
  • what is your suggestion for this line? – cihatid May 24 '20 at 00:02
  • I suggest that you check for `null` and deal with that case. `lastLocation?.let { var lastUserLocation = LatLng(it.latitude, it.longitude); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastUserLocation,17f))` }`, for example. You also need to have `locationListener` handle when you get location events and update the map then too, at least for the first location if `getLastKnownLocation()` returned `null`. – CommonsWare May 24 '20 at 00:13
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – denvercoder9 May 24 '20 at 00:15

0 Answers0