3

I'm trying to add a Place Autocomplete Fragment into my fragment by following the official documentation here

I get the error kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.gms.location.places.ui.PlaceAutocompleteFragment

I get that PlaceAutocompleteFragment can't be set to null, so i've tried added an if statement in my getAutoCompleteSearchResults() to check if fragmentManager != null, but no luck still

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 for the fragment:

<?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>
Barcode
  • 930
  • 1
  • 13
  • 31
  • `onCreate()` is too early to find a nested `Fragment`. Move that to `onViewCreated()`, or possibly `onActivityCreated()`. – Mike M. Jan 18 '19 at 07:12
  • that didn't solve the issue :( – Barcode Jan 18 '19 at 15:13
  • I didn't initially notice that you weren't, but to find a nested `Fragment`, you would need to use `childFragmentManager`. If you got it through the `Activity`'s `FragmentManager`, then your setup is not actually as you've described. That is, the `PlaceAutocompleteFragment` isn't really nested inside your other `Fragment`. – Mike M. Jan 18 '19 at 16:50

2 Answers2

3

Actually error is here :

val autocompleteFragment = fragmentManager?.findFragmentById(R.id.place_autocomplete_fragment2) as PlaceAutocompleteFragment

You're casting a nullable object to non-null receiver type.

Solution :

Make your casting nullable so that cast will never fail but provide null object like below.

val autocompleteFragment = fragmentManager?.findFragmentById(R.id.place_autocomplete_fragment2) as? PlaceAutocompleteFragment // Make casting of 'as' to nullable cast 'as?'

So now, your autocompleteFragment object becomes nullable.

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • I did that, and then had to make the casting of `autocompleteFrament` val to a nullable also in the next line of code like so: `autocompleteFragment?.setOnPlaceSelectedListener`. My app doesn't crash or have any errors anymore, and the search bar autocompletes places for me, but the overrided `onPlaceSelected` function isn't getting called. My log statement isn't displaying the location that is in the search bar – Barcode Jan 18 '19 at 15:10
  • 1
    In such case.. use `childFragmentManager` instead of `fragmentManager`, because you're using fragment inside fragment. That'll fix the problem. – Jeel Vankhede Jan 18 '19 at 15:58
-1

i figured it out. Since i'm trying to find a fragment within a fragment, i have to do the following:

val autocompleteFragment =
        activity!!.fragmentManager.findFragmentById(R.id.place_autocomplete_fragment2) as PlaceAutocompleteFragment

We need to get the parent activity

Barcode
  • 930
  • 1
  • 13
  • 31