Android lateinit属性mapRoute尚未初始化

Android lateinit属性mapRoute尚未初始化,android,dictionary,kotlin,Android,Dictionary,Kotlin,我正在尝试制作一个应用程序,它可以跟踪用户并绘制出用户所走的路线。 每当我尝试在AVD上安装应用程序时,应用程序都会不断崩溃,我会收到错误消息lateinit尚未初始化 这是我的代码: package com.example.map2020 类MapsActivity:AppCompatActivity(),OnMapReadyCallback{ private lateinit var map: GoogleMap private lateinit var mapRoute: Polylin

我正在尝试制作一个应用程序,它可以跟踪用户并绘制出用户所走的路线。 每当我尝试在AVD上安装应用程序时,应用程序都会不断崩溃,我会收到错误消息lateinit尚未初始化

这是我的代码:

package com.example.map2020
类MapsActivity:AppCompatActivity(),OnMapReadyCallback{

private lateinit var map: GoogleMap
private lateinit var mapRoute: PolylineOptions
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var mCurrentLocation: Location
private lateinit var locationRequestTask: Task<LocationSettingsResponse>
private lateinit var locationCallback: LocationCallback
private lateinit var locationRequest: LocationRequest
private var requestingLocationUpdates = true
private val REQUESTING_LOCATION_UPDATES_KEY = "requesting-location-updates-key";

private val TAG = MapsActivity::class.java.simpleName
private val REQUEST_LOCATION_PERMISSION = 1

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
    mapFragment?.getMapAsync(this)

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    locationRequest = LocationRequest.create()?.apply({
        interval = 5000
        fastestInterval = 3000
        priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    })!!

    fusedLocationClient.lastLocation
        .addOnSuccessListener { location : Location? ->
            if (location != null) {
                mCurrentLocation = location
            }
        }

    locationRequestTask = createLocationRequest()

    locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult?) {
            locationResult ?: return
            for (location in locationResult.locations){
                mapRoute.add(LatLng(location.latitude, location.longitude))
                map.addPolyline(mapRoute)
            }
        }
    }
}

override fun onMapReady(googleMap: GoogleMap) {
    map = googleMap
    val zoomLevel = 15f

    val homeLatLng = LatLng(-6.240423, 106.605836)
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(homeLatLng, zoomLevel))
    map.addMarker(MarkerOptions().position(homeLatLng))

    mapRoute = PolylineOptions()
        .width(8f)
        .color(Color.GREEN)
    map.addPolyline(mapRoute)
    enableMyLocation()

}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    val inflater = menuInflater
    inflater.inflate(R.menu.map_options, menu)
    return true
}


// Checks that users have given permission
private fun isPermissionGranted() : Boolean {
    return ContextCompat.checkSelfPermission(
        this,
        Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}

// Checks if users have given their location and sets location enabled if so.
private fun enableMyLocation() {
    if (isPermissionGranted()) {
        map.isMyLocationEnabled = true
    }
    else {
        ActivityCompat.requestPermissions(
            this,
            arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION),
            REQUEST_LOCATION_PERMISSION
        )
    }
}

// Callback for the result from requesting permissions.
// This method is invoked for every call on requestPermissions(android.app.Activity, String[],
// int).
override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<String>,
    grantResults: IntArray) {
    // Check if location permissions are granted and if so enable the
    // location data layer.
    if (requestCode == REQUEST_LOCATION_PERMISSION) {
        if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
            enableMyLocation()
        }
    }
}

fun createLocationRequest(): Task<LocationSettingsResponse> {


    val builder = locationRequest.let {
        LocationSettingsRequest.Builder()
            .addLocationRequest(it)
    }
    val client: SettingsClient = LocationServices.getSettingsClient(this)
    return client.checkLocationSettings(builder.build())
}

override fun onResume() {
    super.onResume()
    if (requestingLocationUpdates) startLocationUpdates()
}

private fun startLocationUpdates() {
    fusedLocationClient.requestLocationUpdates(locationRequest,
        locationCallback,
        Looper.getMainLooper())
}

override fun onPause() {
    super.onPause()
    stopLocationUpdates()
}

private fun stopLocationUpdates() {
    fusedLocationClient.removeLocationUpdates(locationCallback)
}}

因为回调locationCallback在onMapReady之前调用过。我建议您也在onMapReady中调用startLocationUpdates()。在onResume中也应选中isMapReady。您需要标记以将映射完全存储在onMapReady中

kotlin.UninitializedPropertyAccessException: lateinit property mapRoute has not been initialized
    at com.example.map2020.MapsActivity.access$getMapRoute$p(MapsActivity.kt:31)
    at com.example.map2020.MapsActivity$onCreate$3.onLocationResult(MapsActivity.kt:73)
    at com.google.android.gms.internal.location.zzau.notifyListener(Unknown Source)
    at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal(Unknown Source)
    at com.google.android.gms.common.api.internal.ListenerHolder$zaa.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at com.google.android.gms.internal.base.zap.dispatchMessage(Unknown Source)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)