You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用重新打开地图页面时崩溃问题求助

Flutter Google Maps 跨页面打开时崩溃问题排查与解决

问题情况

从任意页面跳转打开地图页面时,应用触发NullPointerException(空数组长度获取失败)崩溃,日志中反复出现preferredRenderer: null提示,多次排查空数组无效,地图仍在多次打开后崩溃,需求是实现地图稳定运行。

崩溃日志

D/MapsInitializer( 3326): preferredRenderer: null
D/zzcb    ( 3326): preferredRenderer: null
I/Google Maps Android API( 3326): Google Play services package version: 230914044
I/Google Maps Android API( 3326): Google Play services maps renderer version(legacy): 203115000
I/PlatformViewsController( 3326): Using hybrid composition for platform view: 2
E/AndroidRuntime( 3326): FATAL EXCEPTION: GLThread 255
E/AndroidRuntime( 3326): Process: com.zone.app, PID: 3326
E/AndroidRuntime( 3326): java.lang.NullPointerException: Attempt to get length of null array
E/AndroidRuntime( 3326):    at java.nio.ByteBufferAsIntBuffer.put(ByteBufferAsIntBuffer.java:122)
E/AndroidRuntime( 3326):    at com.google.maps.api.android.lib6.gmm6.vector.gl.buffer.n.i(:com.google.android.gms.dynamite_mapsdynamite@230914044@23.09.14 (190400-0):2)
E/AndroidRuntime( 3326):  at com.google.maps.api.android.lib6.gmm6.vector.gl.buffer.n.d(:com.google.android.gms.dynamite_mapsdynamite@230914044@23.09.14 (190400-0):3)
E/AndroidRuntime( 3326):    at com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.d.s(:com.google.android.gms.dynamite_mapsdynamite@230914044@23.09.14 (190400-0):2)
E/AndroidRuntime( 3326):    at com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.ao.s(:com.google.android.gms.dynamite_mapsdynamite@230914044@23.09.14 (190400-0):12)
E/AndroidRuntime( 3326):    at com.google.maps.api.android.lib6.gmm6.vector.bx.s(:com.google.android.gms.dynamite_mapsdynamite@230914044@23.09.14 (190400-0):29)
E/AndroidRuntime( 3326):    at com.google.maps.api.android.lib6.gmm6.vector.bq.b(:com.google.android.gms.dynamite_mapsdynamite@230914044@23.09.14 (190400-0):151)
E/AndroidRuntime( 3326):    at com.google.maps.api.android.lib6.gmm6.vector.at.run(:com.google.android.gms.dynamite_mapsdynamite@230914044@23.09.14 (190400-0):48)
D/CompatibilityChangeReporter( 3326): Compat change id reported: 194532703; UID 10278; state: ENABLED
I/TRuntime.CctTransportBackend( 3326): Making request to: https://crashlyticsreports-pa.googleapis.com/v1/firelog/legacy/batchlog
D/TrafficStats( 3326): tagSocket(120) with statsTag=0xffffffff, statsUid=-1
I/TRuntime.CctTransportBackend( 3326): Status Code: 200
D/FlutterGeolocator( 3326): Detaching Geolocator from activity
D/FlutterGeolocator( 3326): Flutter engine disconnected. Connected engine count 0
D/FlutterGeolocator( 3326): Disposing Geolocator services
E/FlutterGeolocator( 3326): Geolocator position updates stopped
D/FlutterGeolocator( 3326): Stopping location service.
D/FlutterGeolocator( 3326): Unbinding from location service.
D/FlutterGeolocator( 3326): Destroying location service.
D/FlutterGeolocator( 3326): Stopping location service.
D/FlutterGeolocator( 3326): Destroyed location service.
Lost connection to device.

可行解决方案

1. 强制指定地图渲染器

preferredRenderer: null是核心诱因之一,初始化时强制指定渲染器类型:

  • Flutter端代码(在使用GoogleMap前调用):
    import 'package:google_maps_flutter_android/google_maps_flutter_android.dart';
    import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
    
    // 初始化时指定legacy渲染器
    await GoogleMapsFlutterAndroid.initializeMapsRenderer(AndroidMapsRenderer.legacy);
    
  • 原生Android端代码(在Application或Activity的onCreate中):
    import com.google.android.gms.maps.MapsInitializer;
    
    MapsInitializer.initialize(getApplicationContext(), MapsInitializer.Renderer.LEGACY, null);
    

2. 严格管理地图页面生命周期

确保页面销毁时释放地图资源,避免内存泄漏:

@override
void dispose() {
  // 销毁地图控制器
  _mapController?.dispose();
  super.dispose();
}

3. 升级依赖版本

  • 更新设备上的Google Play服务到最新版
  • 升级google_maps_flutter插件到最新稳定版,修复已知的渲染和内存bug:
    dependencies:
      google_maps_flutter: ^最新版本号
    

4. 禁用Hybrid Composition

日志显示使用了Hybrid Composition,部分场景下该模式会和地图渲染冲突,尝试禁用:

GoogleMapsFlutterAndroid.useAndroidViewSurface = false;

内容的提问来源于stack exchange,提问作者DENISH BORAD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:07:23