Reputation: 553
This is my code:
Places.initialize(context!!, getString(R.string.google_api_key))
val autocompleteFragment : AutocompleteSupportFragment = fragmentManager?.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment
autocompleteFragment.setCountry("NO")
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS, Place.Field.ADDRESS_COMPONENTS))
autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(place: Place) {
onPlaceSelectedOverride(place)
Log.d("myTag", place.address + " - " + place.addressComponents + " - " + place.latLng)
}
override fun onError(status: Status) {
throw RuntimeException()
}
})
Problem is the fragmentManager. Previously the code was placed inside onCreate() of the mainActivity, and the code had this difference.
val autocompleteFragment : AutocompleteSupportFragment = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment
But on moving the code to another fragment, all sorts of problems came about. fragmentManager appears to be null. What is fragmentManager and how can I make this code function inside a fragment?
Upvotes: 0
Views: 896
Reputation: 973
Try using childFragmentManager
val autocompleteFragment : AutocompleteSupportFragment = childFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment
Upvotes: 1
Reputation: 544
Try this:
val autocompleteFragment : AutocompleteSupportFragment = activity!!supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment
Upvotes: 0
Reputation: 1021
Not sure if you're working with nested fragments, but if it's the case, you can try using childFragmentManager.
Upvotes: 0