为啥尝试在 GoogleMap 上放置标记时出现 NullPointerException?

Posted

技术标签:

【中文标题】为啥尝试在 GoogleMap 上放置标记时出现 NullPointerException?【英文标题】:Why am I getting a NullPointerException When Attempting To Place a Marker on GoogleMap?为什么尝试在 GoogleMap 上放置标记时出现 NullPointerException? 【发布时间】:2021-11-30 03:49:22 【问题描述】:

我在让 Google Map SDK 显示在我的 Kotlin android 项目的片段中时遇到问题。

地图有时会显示,有时不会。我可能会运行该应用程序(绿色播放按钮 Android Studio)一次,它会工作,关闭并再次运行它,它会崩溃,尽管代码本身没有更改。

尝试放置地图标记也经常导致应用程序崩溃,有时它会显示,有时它会崩溃......

这是片段代码:

class global_map : Fragment(), OnMapReadyCallback

    private lateinit var mMap: GoogleMap

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? 
        return inflater.inflate(R.layout.fragment_global_map, container, false)
    

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) 
        super.onViewCreated(view, savedInstanceState)

        val mapFragment: SupportMapFragment = childFragmentManager.findFragmentById(R.id.mapFragment) as SupportMapFragment
        mapFragment.getMapAsync(this)
    
    

    override fun onMapReady(googleMap: GoogleMap) 
       mMap = googleMap
        mMap.addMarker(
            MarkerOptions().position(LatLng(0.0, 0.0)).title("Test")
        )
    

就Logcat的数据而言,请在下面找到:

2021-10-11 14:31:34.747 6751-6751/co.uk.myapp.app W/Resources: Drawable com.google.android.gms.dynamite_mapsdynamite:drawable/maps_watermark_color has unresolved theme attributes! Consider using Resources.getDrawable(int, Theme) or Context.getDrawable(int).
    java.lang.RuntimeException
        at android.content.res.Resources.getDrawable(Resources.java:899)
        at com.google.maps.api.android.lib6.impl.ax.m(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):5)
        at com.google.maps.api.android.lib6.impl.fh.<init>(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):3)
        at com.google.maps.api.android.lib6.impl.bo.az(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):16)
        at com.google.maps.api.android.lib6.impl.cr.o(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):2)
        at com.google.android.gms.maps.internal.o.aX(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):22)
        at dx.onTransact(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):4)
        at android.os.Binder.transact(Binder.java:1043)
        at com.google.android.gms.internal.maps.zza.zzH(com.google.android.gms:play-services-maps@@17.0.1:2)
        at com.google.android.gms.maps.internal.zzk.onCreateView(com.google.android.gms:play-services-maps@@17.0.1:5)
        at com.google.android.gms.maps.zzau.onCreateView(com.google.android.gms:play-services-maps@@17.0.1:7)
        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(com.google.android.gms:play-services-maps@@17.0.1:1)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8024)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
2021-10-11 14:31:34.747 6751-6751/co.uk.myapp.app W/Resources:     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)
2021-10-11 14:31:34.774 6751-6751/co.uk.myapp.app W/Resources: Drawable com.google.android.gms.dynamite_mapsdynamite:drawable/maps_btn_myl has unresolved theme attributes! Consider using Resources.getDrawable(int, Theme) or Context.getDrawable(int).
    java.lang.RuntimeException
        at android.content.res.Resources.getDrawable(Resources.java:899)
        at com.google.maps.api.android.lib6.impl.ax.m(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):5)
        at com.google.maps.api.android.lib6.impl.w.<init>(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):8)
        at com.google.maps.api.android.lib6.impl.bo.az(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):20)
        at com.google.maps.api.android.lib6.impl.cr.o(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):2)
        at com.google.android.gms.maps.internal.o.aX(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):22)
        at dx.onTransact(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):4)
        at android.os.Binder.transact(Binder.java:1043)
        at com.google.android.gms.internal.maps.zza.zzH(com.google.android.gms:play-services-maps@@17.0.1:2)
        at com.google.android.gms.maps.internal.zzk.onCreateView(com.google.android.gms:play-services-maps@@17.0.1:5)
        at com.google.android.gms.maps.zzau.onCreateView(com.google.android.gms:play-services-maps@@17.0.1:7)
        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(com.google.android.gms:play-services-maps@@17.0.1:1)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8024)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
2021-10-11 14:31:34.775 6751-6751/co.uk.myapp.app W/Resources:     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)
2021-10-11 14:31:34.784 6751-6751/co.uk.myapp.app W/Resources: Drawable com.google.android.gms.dynamite_mapsdynamite:drawable/maps_btn_zoom_down has unresolved theme attributes! Consider using Resources.getDrawable(int, Theme) or Context.getDrawable(int).
    java.lang.RuntimeException
        at android.content.res.Resources.getDrawable(Resources.java:899)
        at com.google.maps.api.android.lib6.impl.ax.m(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):5)
        at com.google.maps.api.android.lib6.impl.w.<init>(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):30)
        at com.google.maps.api.android.lib6.impl.bo.az(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):20)
        at com.google.maps.api.android.lib6.impl.cr.o(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):2)
        at com.google.android.gms.maps.internal.o.aX(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):22)
        at dx.onTransact(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):4)
        at android.os.Binder.transact(Binder.java:1043)
        at com.google.android.gms.internal.maps.zza.zzH(com.google.android.gms:play-services-maps@@17.0.1:2)
        at com.google.android.gms.maps.internal.zzk.onCreateView(com.google.android.gms:play-services-maps@@17.0.1:5)
        at com.google.android.gms.maps.zzau.onCreateView(com.google.android.gms:play-services-maps@@17.0.1:7)
        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(com.google.android.gms:play-services-maps@@17.0.1:1)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
        at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
        at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
        at android.app.Activity.performStart(Activity.java:8024)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
2021-10-11 14:31:34.784 6751-6751/co.uk.myapp.app W/Resources:     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)
2021-10-11 14:31:34.932 6751-6774/co.uk.myapp.app W/AdrenoUtils: <ReadGpuID_from_sysfs:197>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
2021-10-11 14:31:34.932 6751-6774/co.uk.myapp.app W/AdrenoUtils: <ReadGpuID:221>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
2021-10-11 14:31:35.014 6751-6774/co.uk.myapp.app W/Gralloc3: mapper 3.x is not supported
2021-10-11 14:31:35.020 6751-6816/co.uk.myapp.app E/AndroidRuntime: FATAL EXCEPTION: GLThread 24197
    Process: co.uk.myapp.app, PID: 6751
    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.b(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):11)
        at com.google.maps.api.android.lib6.gmm6.vector.gl.aj.<init>(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):3)
        at com.google.maps.api.android.lib6.gmm6.vector.gl.g.<init>(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):18)
        at com.google.maps.api.android.lib6.gmm6.vector.bs.d(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):14)
        at com.google.maps.api.android.lib6.gmm6.vector.av.run(:com.google.android.gms.dynamite_mapsdynamite@213614097@21.36.14 (150400-0):45)
2021-10-11 14:31:35.028 1500-2253/? W/ActivityTaskManager:   Force finishing activity co.uk.myapp.app/.MainActivity

【问题讨论】:

由于 NPE 在 API 类内部,我们需要查看更多堆栈跟踪。 感谢@Tenfour04,我已经包含了来自 logcat 的更多数据。我在清单中有我的 API 密钥,在我的 Gradle 中有 implementation 'com.google.android.gms:play-services-maps:17.0.1',我在 YouTube 和 Google 文档(针对 Activity 而不是 Fragment)上遵循了许多教程,但我仍然遇到很多问题。 这很烦人,因为有时它会在我运行它时工作一次,然后如果我应用更改并再次构建它,它就会崩溃 【参考方案1】:

这可能是也可能不是问题的根源。只是猜测。

由于地图是异步创建的,因此可能会在 Fragment 处于分离状态时调用 onMapReady,在这种情况下,地图视图将不会使用关联的上下文和主题。也许这导致它在检索其可绘制资源时出错。我会将您的代码更改如下:

override fun onMapReady(googleMap: GoogleMap) 
    context ?: return // Fragment has been detached--don't do anything.
    mMap = googleMap
    mMap.addMarker(
        MarkerOptions().position(LatLng(0.0, 0.0)).title("Test")
    )

或者:

override fun onMapReady(googleMap: GoogleMap) 
    if (isDetached) return
    mMap = googleMap
    mMap.addMarker(
        MarkerOptions().position(LatLng(0.0, 0.0)).title("Test")
    )

【讨论】:

感谢您的回答。这当然可以解释为什么有时它可以在构建上工作,而其他时候却不行。我会尽快报告 不幸的是,我仍然遇到同样的错误,NPE 与可绘制对象有关。我将在我的另一个项目中尝试该代码,看看它是否也一样。 如果有帮助,如果我清除应用程序的缓存,应用程序实际上会恢复正常工作。如果我在 Android Studio 中使用“Apply Changes and Restart Activty”,它会导致它再次崩溃(直到缓存被清除) 哦,这完全在意料之中! “应用更改并重新启动”有时会起作用,因为它试图在不重新编译的情况下热交换代码。任何影响资源的更改都会导致它无法工作。还有其他类型的更改也会导致它失败,例如从类中添加或删除属性或更改函数签名。这在 Android Studio 的开发人员指南中有记录。大多数时候你应该使用 Run 来重建你的应用程序。应用更改仅适用于 Java/Kotlin 代码中的微小更改。 好的,非常感谢,我担心如果我在 Play 商店上发布它,我必须继续清除我的缓存以及任何用户都必须这样做。

以上是关于为啥尝试在 GoogleMap 上放置标记时出现 NullPointerException?的主要内容,如果未能解决你的问题,请参考以下文章

我的函数 get Nearby Restaurant 未被调用以在 Google Map 上放置标记

Sencha Touch - 谷歌地图和居中标记

从 GoogleMap 中删除标记

使用 R googlemap 包按集群收集标记

SVN使用时常常出现红色标记的,更新也是正常的,为啥会这样?怎么解决?

情节标记图例在情节上多次出现