未设置PlaceSelectionListener.不会提供任何结果



当我单击autocompletefragment时。日志显示:

位置:未设置PlaceSelectionListener。不会有结果交付。

val realView = inflater.inflate(R.layout.fragment_connect, container, false)
Places.initialize(activity!!.applicationContext, getString(R.string.google_maps_key))
val placesClient = Places.createClient(activity!!)
val autocompleteFragment1 = fragmentManager?.findFragmentById(R.id.autocomplete_fragment1) as? AutocompleteSupportFragment // Make casting of 'as' to nullable cast 'as?'
autocompleteFragment1?.setCountry("PH")
// Specify the types of place data to return.
autocompleteFragment1?.setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME))
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment1?.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(place: Place) {
Log.d("Hey", place.toString())
}
override fun onError(status: Status) {
// TODO: Handle the error.
Log.d("HOY", "An error occurred: ${status.statusCode}")
}
})

您使用的是不推荐使用的代码。尝试替换此行:

val autocompleteFragment1 = fragmentManager?.findFragmentById(R.id.autocomplete_fragment1) as? AutocompleteSupportFragment // Make casting of 'as' to nullable cast 'as?'

有了这个:

val autocompleteFragment1 = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment1) as? AutocompleteSupportFragment // Make casting of 'as' to nullable cast 'as?

在上面的更改之后,你的确切代码对我来说是有效的,所以我希望这能有所帮助!

相关内容

  • 没有找到相关文章