how to solve Android Place Autocomplete intent issue?

655 Views Asked by At

I'm trying to use google Place Autocomplete in my app.

Like described in the documentation, there is two ways to use this feature : one using a fragment and the other way is to use an Intent and that's what I choose.

My code is like below

Gradle:

implementation 'com.google.android.libraries.places:places:2.5.0'

Kotlin:

Part 1

 btn.setOnClickListener {

            if (!Places.isInitialized()) {
                Places.initialize(
                    requireActivity().applicationContext,
                    getString(R.string.google_maps_key)
                )
            }

            @Suppress("DEPRECATION")
            startActivityForResult(
                Autocomplete
                    .IntentBuilder(
                        AutocompleteActivityMode.OVERLAY,
                        listOf(
                            Place.Field.ID,
                            Place.Field.NAME,
                            Place.Field.LAT_LNG,
                            Place.Field.ADDRESS,
                            Place.Field.ADDRESS_COMPONENTS
                        )
                    ).build(requireContext()),
                AUTOCOMPLETE_REQUEST_CODE
            )
        }

Part 2

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
            when (resultCode) {
                Activity.RESULT_OK -> {
                    data?.let {
                        val place = Autocomplete.getPlaceFromIntent(data)
                        Timber.i(TAG, "Place: ${place.name}, ${place.id}")
                    }
                }
                AutocompleteActivity.RESULT_ERROR -> {
                    // TODO: Handle the error.
                    data?.let {
                        val status = Autocomplete.getStatusFromIntent(data)
                        Timber.i(TAG, status.statusMessage)
                    }
                }
                Activity.RESULT_CANCELED -> {
                    // The user canceled the operation.
                }
            }
            return
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

The overlay 'Place Auto Complete' view appear perfectly but when I try to tape in the keyboard to search for some place, the view crashes not the app. only the view and I got the error below (I don't know if it is related) :

set_timerslack_ns write failed: Operation not permitted

Any idea how to solve this ?

1

There are 1 best solutions below

3
Christos On

Did you "enable" your api key for using the google places?