Android Studio 在onLocationResult上运行应用程序时出错,如何修复?

mcvgt66p  于 2023-11-21  发布在  Android
关注(0)|答案(1)|浏览(115)
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    // Define an enum for marker types
    enum class MarkerType(val resourceId: Int, val scale: Float) {
        DEFAULT(-1, 1.0f), // Use -1 to indicate no custom marker
        CUSTOM_1(R.drawable.icon_circle, 0.4f),
        CUSTOM_2(R.drawable.circle_arrow, 0.6f)
    }

    private lateinit var mMap: GoogleMap
    private lateinit var binding: ActivityMapsBinding
    private val LOCATION_PERMISSION_REQUEST_CODE = 1
    private var points: ArrayList<LatLng>? = null
    private var line: Polyline? = null
    private lateinit var rotatingMarker: RotatingMarker
    private lateinit var locationClient: FusedLocationProviderClient
    private var userLocation: LatLng = LatLng(0.0, 0.0)

    // Location callback defined here
    private val locationCallback = object : LocationCallback() {
        fun onLocationResult(locationResult: LocationResult?) {
            locationResult?.lastLocation?.let { location ->
                // Handle the new location here
                val newLocation = LatLng(location.latitude, location.longitude)
                updateMarkerPosition(newLocation)
            }
        }
    }

字符串
错误/S:
e:file:/D:/Android_Studio_Projects/Live_Route/app/src/main/java/com/example/liveroute/MapsActivity.kt:55:9意外覆盖:以下声明具有相同的JVM签名(onLocationResult(Lcom/google/android/gms/location/LocationResult;)V):fun onLocationResult(p0:LocationResult):Unit defined in com. example. liveroute. MapsActivity. <no name provided> fun onLocationResult(locationResult:LocationResult?):Unit defined in com. example. liveroute. MapsActivity. <no name provided>
当我在乐趣之前添加覆盖时,比如:

private val locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult?) {
            locationResult?.lastLocation?.let { location ->
                // Handle the new location here
                val newLocation = LatLng(location.latitude, location.longitude)
                updateMarkerPosition(newLocation)
            }
        }
    }


我就会在超驰上出错
'onLocationResult'不覆盖任何内容

c0vxltue

c0vxltue1#

错误“onLocationResult overrides nothing”是因为您忘记在覆盖中调用super.onLocationResult()而导致的。
因此,根据您的情况,添加super调用。

private val locationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult?) {
        super.onLocationResult() <--- HERE
        locationResult?.lastLocation?.let { location ->
            // Handle the new location here
            val newLocation = LatLng(location.latitude, location.longitude)
            updateMarkerPosition(newLocation)
        }
        super.onLocationResult() <--- OR HERE
    }
}

字符串
记住在覆盖中包含super调用。
编辑
如果你不想叫管理员:
发件人:

override fun onLocationResult(locationResult: LocationResult?)


收件人:

override fun onLocationResult(locationResult: LocationResult)


删除函数参数中LocationResult的可空值(?符号)。
同样,这样做,你不必调用super.onLocationResult()
你可以从官方文档中找到更多关于super和classes的信息。

相关问题