Reputation: 458
I'm creating a Google Maps Places API Client using the following code:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!Places.isInitialized()) {
Places.initialize(this.requireActivity(), BuildConfig.MAP_API_KEY)
}
placesClient = Places.createClient(requireActivity())
}
However, exiting the map activity results in the following error:
\*\\\~\*\\\~\*\\\~
Previous channel {0} was garbage collected without being shut down!
\\\~\*\\\~\*\\\~\*
Make sure to call shutdown()/shutdownNow()
java.lang.RuntimeException: ManagedChannel allocation site
at com.google.android.libraries.places.internal.zzbme.\\\<init\\\>(com.google.android.libraries.places:places@@3.5.0:3)
at com.google.android.libraries.places.internal.zzbmf.\\\<init\\\>(com.google.android.libraries.places:places@@3.5.0:2)
at com.google.android.libraries.places.internal.zzbmd.zza(com.google.android.libraries.places:places@@3.5.0:18)
at com.google.android.libraries.places.internal.zzazt.zza(com.google.android.libraries.places:places@@3.5.0:1)
at com.google.android.libraries.places.internal.zzjn.zza(com.google.android.libraries.places:places@@3.5.0:26)
at com.google.android.libraries.places.api.Places.zza(com.google.android.libraries.places:places@@3.5.0:7)
at com.google.android.libraries.places.api.Places.createClient(com.google.android.libraries.places:places@@3.5.0:3)
at com.FragmentMapLocation.onCreate(FragmentChargerMapLocation.kt:71) at androidx.fragment.app.Fragment.performCreate(Fragment.java:3094) at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:504)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:268)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
...
I tried to use the code below but even after making null getting same error.
override fun onStop() {
super.onStop()
placesClient = null
}
override fun onDestroy() {
super.onDestroy()
// Shut down the PlacesClient
try {
(placesClient as? AutoCloseable)?.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
Upvotes: 3
Views: 220
Reputation: 558
The problem with your code is that you aren't supposed to close the PlacesClient
- you should use a de-initialization method of Places
instead. Consider the following code:
class MyActivity : ComponentActivity() {
lateinit var placesClient: PlacesClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Places.initialize(applicationContext, BuildConfig.MAP_API_KEY)
placesClient = Places.createClient(this)
}
override fun onDestroy() {
super.onDestroy()
Places.deinitialize()
}
}
For more information about Places, you can refer to the docs.
Upvotes: 0