i am on android for 2 weeks now. As my first project i tried to set up a Toolbar with a switch and the placeautocompletefragment, followed by a mapfragment. So the map works fine, the toolbar too, except the Placeautocompletefragment in it. While debugging, i found out, after initializing the Placeautocompletefragment it is still null.
the places and map api's are activated, the key in the manifest.
Could somebody please tell me, where i made the mistake and tell me how to solve it? I cant set a listener on it cause of the NULL value. Each samples i found in the internet are with the fragmentmanager, which is deprecated since API 28, so actually useless for me.
Thanks so far.
Greetz
Edit: ativity_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="20dp"
app:title="Fake GPS"
android:visibility="visible">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</android.support.v7.widget.Toolbar>
<fragment android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
Mapsactivity.kot
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment!!.getMapAsync(this)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
onOffSwitch = menu.findItem(R.id.switchIO).actionView as Switch
onOffSwitch?.setOnCheckedChangeListener { _, isChecked -> (removed code here, cause unnecessary) }
autocompleteFragment = supportFragmentManager.findFragmentById(R.id.place_autocomplete_fragment) as? PlaceAutocompleteFragment
}
}