如何解决 Android Place Autocomplete 意图问题?

发布于 2025-01-12 06:49:53 字数 2522 浏览 5 评论 0原文

我正在尝试在我的应用中使用 google 地点自动完成

正如文档中所述,有两种方法可以使用此功能:一种使用片段,另一种方法是使用 Intent,这就是我选择的方法。

我的代码如下所示

Gradle:

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

Kotlin:

第 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
            )
        }

第 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)
    }

出现叠加层“放置自动完成”视图完美,但是当我尝试用胶带粘贴键盘来搜索某个地方时,视图崩溃而不是应用程序崩溃。只有视图,我收到以下错误(我不知道它是否相关):

set_timerslack_ns write failed: Operation not permitted

知道如何解决这个问题吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漆黑的白昼 2025-01-19 06:49:53

您是否“启用”了 api 密钥以使用 google 地点?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文