使用用于 GoogleMaps 的 android studio 样板代码的运行时错误
Posted
技术标签:
【中文标题】使用用于 GoogleMaps 的 android studio 样板代码的运行时错误【英文标题】:Runtime Errors using android studio boilerplate code for GoogleMaps 【发布时间】:2021-10-31 05:55:01 【问题描述】:我在模拟器(Pixel 4 API 30)上运行基本的谷歌地图演示时遇到问题。有时它可以工作,但有时我会遇到两个错误之一。我已经使用 this 指南设置了 API 密钥,而不是仅仅将它放在基本代码中提供的 google_maps_api.xml 中。
清单
<?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_
android:layout_
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))
错误信息一
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.joe.googlemapsdemo, PID: 10408
java.lang.RuntimeException: Unable to start activity ComponentInfocom.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@213016100@21.30.16 (150700-0):5)
at com.google.maps.api.android.lib6.impl.fh.<init>(:com.google.android.gms.dynamite_mapsdynamite@213016100@21.30.16 (150700-0):6)
at com.google.maps.api.android.lib6.impl.bo.az(:com.google.android.gms.dynamite_mapsdynamite@213016100@21.30.16 (150700-0):16)
at com.google.maps.api.android.lib6.impl.cr.o(:com.google.android.gms.dynamite_mapsdynamite@213016100@21.30.16 (150700-0):2)
at com.google.android.gms.maps.internal.o.aX(:com.google.android.gms.dynamite_mapsdynamite@213016100@21.30.16 (150700-0):22)
at dx.onTransact(:com.google.android.gms.dynamite_mapsdynamite@213016100@21.30.16 (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)
错误信息二
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@201817052@20.18.17 (040700-0):11)
at com.google.maps.api.android.lib6.gmm6.vector.gl.aj.<init>(:com.google.android.gms.dynamite_mapsdynamite@201817052@20.18.17 (040700-0):3)
at com.google.maps.api.android.lib6.gmm6.vector.gl.g.<init>(:com.google.android.gms.dynamite_mapsdynamite@201817052@20.18.17 (040700-0):23)
at com.google.maps.api.android.lib6.gmm6.vector.bt.b(:com.google.android.gms.dynamite_mapsdynamite@201817052@20.18.17 (040700-0):20)
at com.google.maps.api.android.lib6.gmm6.vector.av.run(:com.google.android.gms.dynamite_mapsdynamite@201817052@20.18.17 (040700-0):56)
我没有改变任何东西来获得不同的错误消息,这非常令人困惑,更令人困惑的是有时它会起作用。我尝试将我的 API 密钥重新定位到 google_maps_api.xml 并从我的模拟器中擦除数据。我已经看到与此类似的问题,但似乎没有解决方案匹配/有效。
【问题讨论】:
关于这个主题的任何更新?我不断收到您的第二条消息...并且以某种方式在重新启动应用程序时触发了分段错误 恐怕不在我这边。 我在 google 中创建了一个问题,我会在此处留下链接,以防有进一步的进展,希望对您有所帮助:issuetracker.google.com/issues/201308676 【参考方案1】:在activit_maps.xml中,尝试改变: com.google.android.gms.maps.SupportMapFragment 到 com.google.android.gms.maps.MapFragment
【讨论】:
感谢您的建议。我看过与此相反的帖子。我似乎没有解决任何问题,我仍然只是收到错误消息。以上是关于使用用于 GoogleMaps 的 android studio 样板代码的运行时错误的主要内容,如果未能解决你的问题,请参考以下文章
带有多个标记的javascript googlemaps没有使用cordova应用程序在android中显示标记
用于颤振的 GoogleMaps 插件仅显示 ios 模拟器上的标记
如何使用 Android Studio 将信息窗口放置在 GoogleMaps 中标记的左侧或右侧
Android - SupportMapFragment 与 GoogleMaps API 2.0 给出 IllegalArgumentException