- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在按照官方文档 here 尝试将一个 Place Autocomplete fragment 添加到我的 fragment 中
我收到错误 kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
我知道 PlaceAutocompleteFragment 不能设置为 null,所以我尝试在我的 getAutoCompleteSearchResults()
中添加一个 if 语句来检查 fragmentManager != null,但还是不行
AddLocationFragment.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
getAutoCompleteSearchResults()
}
private fun getAutoCompleteSearchResults() {
val autocompleteFragment =
fragmentManager?.findFragmentById(R.id.place_autocomplete_fragment2) as PlaceAutocompleteFragment
autocompleteFragment.setOnPlaceSelectedListener(object : PlaceSelectionListener {
override fun onPlaceSelected(place: Place) {
// TODO: Get info about the selected place.
Log.i(AddLocationFragment.TAG, "Place: " + place.name)
}
override fun onError(status: Status) {
Log.i(AddLocationFragment.TAG, "An error occurred: $status")
}
})
}
}
fragment 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
tools:context=".AddLocationFragment" tools:layout_editor_absoluteY="81dp">
<fragment
android:id="@+id/place_autocomplete_fragment2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:theme="@style/AppTheme"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/etAddress"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
最佳答案
其实错误在这里:
val autocompleteFragment = fragmentManager?.findFragmentById(R.id.place_autocomplete_fragment2) as PlaceAutocompleteFragment
您正在将一个可空对象转换为非空接收者类型。
解决方案:
使您的转换可空,这样转换就永远不会失败,但会提供空对象,如下所示。
val autocompleteFragment = fragmentManager?.findFragmentById(R.id.place_autocomplete_fragment2) as? PlaceAutocompleteFragment // Make casting of 'as' to nullable cast 'as?'
现在,您的 autocompleteFragment
对象变为可空。
关于android - PlaceAutocompleteFragment - 不能将 null 转换为非 null 类型 (Kotlin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54249056/
我想实现 AutocompleteTextView(google places),但是当我点击 fragment 中的 searchView 时, fragment 消失了(掉下来)。 我的代码:
有没有什么方法可以使用 Google Places API 中的 placeautocompletefragment 获取在搜索 View 中显示的完整地址?它只显示整个地址的第一部分。我确信一切都已
在我的应用程序中,我添加了 Google map ,它们运行良好。我想添加一个 PlaceAutocompleteFragment。我正在使用我已经为 map 生成的 key ,并且我已经从 goog
如何解决PlaceAutocompleteFragment? 我正在尝试使用新的 PlaceAutocompleteFragment。我尝试使用以下代码: 此代码示例只是一个副本。来自这里site
这是我的 .xml 文件 我添加了 PlaceAutocompleteFragment,但它显示未解析的类 PlaceAutocompleteFragment。我检查了依赖文件,所有的东西都被添加了。
在我的应用程序主页中,google place auto complete fragment 在对话框栏内按下按钮打开,该位置用于搜索查询。问题是当我尝试通过再次按下按钮来更改位置时,应用程序崩溃并出
我在 setHint 方法上收到 NPE。Logcat 显示此错误: void android.widget.EditText.setHint(java.lang.CharSequence)' on
我想在 Activity 加载完成后立即自动点击 fragment 。 fragment 定义为: 我试过这样做 fragment = findViewById(R.id.place_autoco
我在最近的一个项目中使用了 Google 的 PlaceAutoCompleteFragment,目前我正在从事这项工作。当我快速单击 PlaceAutoCompleteFragment 时,它会在我
我想在我的 Android 应用程序中实现 placeautocompletefragment。我正在尝试使用 this url 中的说明但它显示: can not resolve symbol fo
我正在使用 PlaceAutocompleteFragment 来搜索地点。下面是我的xml代码 我想删除默认带有 fragment 的搜索图标。 最佳答案 您可以使用这一行来获取搜索图标的引用:(
我正在使用 Google Places API for Android - PlaceAutocompleteFragment 我项目中用于搜索位置的 API,当用户选择一个位置然后使用位置经纬度获取
我正在使用 Google Place获取地点详情。 Google 提供了不同的方式来实现 Google Place API 以获取地点详细信息。不同的方式如 PlaceAutocompleteFrag
我在 Fragment 中使用 PlaceAutoCompleteFragment。第一次打开 Fragment(放置 PlaceAutoCompleteFragment 的 App fragment
我想实现 PlaceAutocompleteFragment based on the docs,在我的应用程序中,但是当我添加此代码段时 最后一行抛出“无法解析符号‘PlaceAutocomple
我已经在 Activity 中实现了 PlaceAutocompleteFragment 并且工作成功。但是如何在android的 fragment 中实现相同的功能?我已经实现了这样的 placea
我正在尝试将 PlaceAutocompleteFragment 添加到我的 Activity 中,这是我正在使用的所有导入: import com.google.android.gms.common
我在 fragment 中使用 PlaceAutoCompleteFragment。第一次打开 Fragment(其中放置了 PlaceAutoCompleteFragment 的 App fragm
我正在按照官方文档 here 尝试将一个 Place Autocomplete fragment 添加到我的 fragment 中 我收到错误 kotlin.TypeCastException: nu
我正在尝试使用 Google 的地点自动完成 ( https://developers.google.com/places/android-api/autocomplete ) 在我的应用程序中的 f
我是一名优秀的程序员,十分优秀!