I'm having trouble with running a basic google maps demo on an emulator (Pixel 4 API 30). Sometimes it works but other times I get one of two errors. I've set up the API key using this guide as opposed to just putting it in the google_maps_api.xml provided in the base code.
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joe.googlemapsdemo">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the "MyLocation" functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GoogleMapsDemo">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />
<activity
android:name=".MapsActivity"
android:exported="true"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activtiy_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
MapsActivity.kt
package com.joe.googlemapsdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.joe.googlemapsdemo.databinding.ActivityMapsBinding
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
private lateinit var binding: ActivityMapsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMapsBinding.inflate(layoutInflater)
setContentView(binding.root)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val osaka = LatLng(34.677518,135.3459511)
mMap.addMarker(MarkerOptions().position(osaka).title("Marker in Osaka"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(osaka))
}
}
Error message one
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.joe.googlemapsdemo, PID: 10408
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joe.googlemapsdemo/com.joe.googlemapsdemo.MapsActivity}: android.view.InflateException: Binary XML file line #9 in com.joe.googlemapsdemo:layout/activity_maps: Binary XML file line #9 in com.joe.googlemapsdemo:layout/activity_maps: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.InflateException: Binary XML file line #9 in com.joe.googlemapsdemo:layout/activity_maps: Binary XML file line #9 in com.joe.googlemapsdemo:layout/activity_maps: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9 in com.joe.googlemapsdemo:layout/activity_maps: Error inflating class fragment
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070016 type #0x3 is not valid
at android.content.res.Resources.getDimensionPixelSize(Resources.java:827)
at com.google.maps.api.android.lib6.impl.ax.d(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150700-0):5)
at com.google.maps.api.android.lib6.impl.fh.<init>(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150700-0):6)
at com.google.maps.api.android.lib6.impl.bo.az(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150700-0):16)
at com.google.maps.api.android.lib6.impl.cr.o(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150700-0):2)
at com.google.android.gms.maps.internal.o.aX(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150700-0):22)
at dx.onTransact(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (150700-0):4)
at android.os.Binder.transact(Binder.java:1043)
at com.google.android.gms.internal.maps.zza.zza(Unknown Source:10)
at com.google.android.gms.maps.internal.zzj.onCreateView(Unknown Source:29)
at com.google.android.gms.maps.SupportMapFragment$zza.onCreateView(Unknown Source:29)
at com.google.android.gms.dynamic.zad.zaa(Unknown Source:4)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.zaa(Unknown Source:9)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.onCreateView(Unknown Source:25)
at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source:28)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:386)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:281)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:140)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.inflate(LayoutInflater.java:657)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.joe.googlemapsdemo.databinding.ActivityMapsBinding.inflate(ActivityMapsBinding.java:36)
at com.joe.googlemapsdemo.databinding.ActivityMapsBinding.inflate(ActivityMapsBinding.java:30)
at com.joe.googlemapsdemo.MapsActivity.onCreate(MapsActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Error message two
E/AndroidRuntime: FATAL EXCEPTION: GLThread 688
Process: com.joe.googlemapsdemo, PID: 8853
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at com.google.maps.api.android.lib6.gmm6.vector.gl.aj.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-0):11)
at com.google.maps.api.android.lib6.gmm6.vector.gl.aj.<init>(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-0):3)
at com.google.maps.api.android.lib6.gmm6.vector.gl.g.<init>(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-0):23)
at com.google.maps.api.android.lib6.gmm6.vector.bt.b(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-0):20)
at com.google.maps.api.android.lib6.gmm6.vector.av.run(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (040700-0):56)
I'm not changing anything to get different error messages which is very confusing, what's even more confusing is that sometimes it will work. I tried relocating my API key to the google_maps_api.xml and wiping the data from my emulator. I have seen similar problems to this but no solutions seem to match/work.