gpt4 book ai didi

Kotlin:无法调用表达式,因为找不到函数 invoke()

转载 作者:行者123 更新时间:2023-12-02 12:15:22 26 4
gpt4 key购买 nike

我正在尝试构建一个实现 Google map 的应用程序。由于某种原因,我收到无法调用表达式的错误,因为找不到函数 invoke() 。我不知道如何解决这个问题,也许你们中的一个可以帮助?

    package com.example.maxs.kotlinnearbyv2

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.example.maxs.kotlinnearbyv2.Common.Common
import com.example.maxs.kotlinnearbyv2.Model.MyPlaces
import com.example.maxs.kotlinnearbyv2.Remote.IGoogleAPIService
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.BitmapDescriptorFactory

import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import kotlinx.android.synthetic.main.activity_maps.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap

private var latitude:Double=0.toDouble()
private var longitude:Double=0.toDouble()

lateinit var mService:IGoogleAPIService

internal var currentPlace: MyPlaces?=null

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)

//Init Service
mService = Common.googleApiService

bottom_navigation_view.setOnNavigationItemReselectedListener {item ->
when(item.itemId)
{
R.id.action_hospital -> nearByPlace("hospital")
R.id.action_restaurant -> nearByPlace("restaurant")
R.id.action_market -> nearByPlace("market")
R.id.action_school -> nearByPlace("school")
}
}
}

private fun nearByPlace(typePlace: String) {

//Clear all marker on Map
mMap.clear()
//build URL request base on location
val url = getUrl(latitude,longitude, typePlace)

mService.getNearByPlaces(url)
.enqueue(object : Callback<MyPlaces>{
override fun onResponse(call: Call<MyPlaces>, response: Response<MyPlaces>) {

currentPlace = response.body()

if(response!!.isSuccessful)
{
for(i in 0 until response!!.body()!!.results!!.size)
{
val markerOptions=MarkerOptions()
val googlePlace = response.body().results!!(i)
val lat = googlePlace.geometry!!.location!!.lat
val lng = googlePlace.geometry!!.location!!.lng
val placeName = googlePlace.name
val latLng = LatLng(lat, lng)

markerOptions.position(latLng)
markerOptions.title(placeName)
if (typePlace.equals("hospital"))
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_local_hospital_black_24dp))
else if (typePlace.equals("market"))
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_shopping_cart_black_24dp))
else if (typePlace.equals("restaurant"))
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_restaurant_black_24dp))
else if (typePlace.equals("school"))
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_school_black_24dp))
else
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))

markerOptions.snippet(i.toString())

//add marker to map
mMap!!.addMarker(markerOptions)


}
//move camera
mMap!!.moveCamera(CameraUpdateFactory.newLatLng(LatLng(latitude, longitude)))
mMap!!.animateCamera(CameraUpdateFactory.zoomTo(15.0f))
}
}

override fun onFailure(call: Call<MyPlaces>, t: Throwable) {
Toast.makeText(baseContext, ""+t!!.message,Toast.LENGTH_SHORT).show()
}

})
}

private fun getUrl(latitude: Double, longitude: Double, typePlace: String): String {

val googlePlaceUrl = StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json")
googlePlaceUrl.append("?location=$latitude,$longitude")
googlePlaceUrl.append("&radius=10000") //10 km
googlePlaceUrl.append("&type=$typePlace")
googlePlaceUrl.append("&key=")

Log.d("URL_DEBUG", googlePlaceUrl.toString())
return googlePlaceUrl.toString()
}

/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap

// Add a marker in Sydney and move the camera
val barbier = LatLng(52.391274, 6.449712)
mMap.addMarker(MarkerOptions().position(barbier).title("Marker in Barbier"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(barbier))
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(barbier, 15.0f))
}
}

我似乎找不到任何解决方案,而且我可能觉得很难……
错误是在 response.body()!!.results!!(i)
val googlePlace = response.body().results!!(i)

它现在肯定让我发疯。

最佳答案

要访问数组或列表中的元素,请使用方括号,例如:

array[i]
list[i] // or list.get(i)
results!![i]

关于错误消息:Kotlin 假设 invoke operator ,你没有提供。您可能想了解调用运算符的用途。有时它会派上用场。但是对于您的问题,方括号就足够了。

作为(进一步的)旁注:不要用大量 !! 编写代码,而是尝试确定什么是 null首先,如果它不适合您的需要,则省略其余部分,例如:
response?.also {
if (it.isSuccessful) {
it.body()?.results?.forEach {
//...
}
}
}

只是一个开始...然后您可能想进一步简化事情...只需省略 !!只要有可能...您可能还想了解 null safety in Kotlin也许还有关于 smart casts .

您的 typePlace.equals(... -条件也可以完美地替换为 when ,例如:
when(typePlace) {
"hospital" -> ...
"market" -> ...

这结合了 letalso甚至可以进一步减少您的代码,但这可能是另一个故事,更适合 code review .

关于Kotlin:无法调用表达式,因为找不到函数 invoke(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53086473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com