I'am new in kotlin and android studio. The problem comes I try to push a button "GPS" from "MainActivity" to "GpsView" (it's other activity);here you can see a map like googleMap, but I try run and push the button and I have Error: android.view.InflateException.
I don't konw how fix this error.
In Build.gradle -> dependencies, I paste this
implementation 'com.google.android.gms:play-services-maps:18.0.0'
In AndroidManifest add this
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
the code of MainActivity :
package com.eleczero.hellokotlin
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun buttongps(view:View){
val intent = Intent(this,GpsView::class.java)
startActivity(intent)
}
}
the layout of the MainActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<Button
android:id="@+id/btngps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttongps"
android:text="GPS"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="323dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
The GpsView code is
package com.eleczero.hellokotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
class GpsView : AppCompatActivity(), OnMapReadyCallback {//
private lateinit var map: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_gps_view)
createFracment()
}
private fun createFracment() {
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
}
the layout is
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>