PlaceAutocompleteFragment - 不能将 null 转换为非 null 类型 (Kotlin)
PlaceAutocompleteFragment - null cannot be cast to non-null type (Kotlin)
我正在尝试按照官方文档 here
将 Place Autocomplete 片段添加到我的片段中
我收到错误 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")
}
})
}
}
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
您正在将 nullable 对象转换为 non-null 接收者类型。
解决方案:
使您的转换 nullable 以便转换永远不会失败,但提供 null 对象,如下所示。
val autocompleteFragment = fragmentManager?.findFragmentById(R.id.place_autocomplete_fragment2) as? PlaceAutocompleteFragment // Make casting of 'as' to nullable cast 'as?'
现在,您的 autocompleteFragment
对象变为 nullable。
我明白了。由于我试图在一个片段中找到一个片段,我必须执行以下操作:
val autocompleteFragment =
activity!!.fragmentManager.findFragmentById(R.id.place_autocomplete_fragment2) as PlaceAutocompleteFragment
我们需要获取父级 activity
我正在尝试按照官方文档 here
将 Place Autocomplete 片段添加到我的片段中我收到错误 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")
}
})
}
}
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
您正在将 nullable 对象转换为 non-null 接收者类型。
解决方案:
使您的转换 nullable 以便转换永远不会失败,但提供 null 对象,如下所示。
val autocompleteFragment = fragmentManager?.findFragmentById(R.id.place_autocomplete_fragment2) as? PlaceAutocompleteFragment // Make casting of 'as' to nullable cast 'as?'
现在,您的 autocompleteFragment
对象变为 nullable。
我明白了。由于我试图在一个片段中找到一个片段,我必须执行以下操作:
val autocompleteFragment =
activity!!.fragmentManager.findFragmentById(R.id.place_autocomplete_fragment2) as PlaceAutocompleteFragment
我们需要获取父级 activity