首页 > 解决方案 > “E/ViewRootImpl:sendUserActionEvent() mView 返回”,而我尝试在自动完成搜索栏中输入(Google Places API)

问题描述

我正在设计一个非常简单的应用程序,其中有一个编辑文本并单击它将打开一个 Google Places API 自动完成搜索栏。我已经生成了 API 密钥并将账单信息添加到我的帐户中。我仍然遇到此错误E/ViewRootImpl: sendUserActionEvent() mView returned并且自动完成搜索栏崩溃。有人可以指导我。一些代码片段->

Kotlin 代码 ->

class MainActivity : AppCompatActivity() {
    companion object{
        private const val PLACE_AUTOCOMPLETE_REQUEST_CODE =3
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if(!Places.isInitialized()){
            Places.initialize(this,resources.getString(R.string.map))
        }
        et_location.setOnClickListener {
            
            // These are the list of fields which we required is passed
            val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS)
            // Start the autocomplete intent with a unique request code.
            val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this)
                startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE)
        } 
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if(resultCode== Activity.RESULT_OK){
            if(requestCode== PLACE_AUTOCOMPLETE_REQUEST_CODE){
                val place : Place = Autocomplete.getPlaceFromIntent(data!!)
                et_location.setText(place.address)
            }
        }
    }
}

XML 代码 ->

<androidx.constraintlayout.widget.ConstraintLayout

xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_location"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="MissingConstraints">

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/et_location"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="location"
            android:inputType="text"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:textSize="18dp" />
    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

一些清单代码->

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
.
.
.
<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/map" />

标签: androidkotlin

解决方案


推荐阅读