You need to enable JavaScript to run this app. Android开发:Google Maps集成Firestore时调用getGeoPoint()返回null触发空指针
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:Google Maps集成Firestore时调用getGeoPoint()返回null触发空指针

问题根因定位

崩溃触发点为setCameraView()方法,报错为调用空对象的getGeo_point()方法,核心原因是异步数据加载时序不匹配:

  • onMapReady()触发时,Firestore的位置数据回调尚未执行,mUserLocations为空,mUserPosition从未被赋值
  • setUserPosition()仅在onCreate()中同步调用一次,此时getUsers()的异步回调还未返回数据,遍历空的mUserLocations自然不会给mUserPosition赋值
  • 重复调用getUsers()会生成多个SnapshotListener,导致mUserLocations重复堆积无效数据
修复步骤

1. 移除无效的同步调用

删除onCreate()中提前执行的加载逻辑:

// 修改后的onCreate()核心代码
mDb = FirebaseFirestore.getInstance();
rvUsers = (RecyclerView) findViewById(R.id.user_list_recycler_view);
initUserRecyclerView();
// 删除原有的两行调用:
// getUsers();
// setUserPosition();

2. 修复重复注册监听问题

在getUsers()开头先清理旧的监听器,避免同一监听重复触发:

private void getUsers() {
    // 新增清理逻辑
    if (mUserListEventListener != null) {
        mUserListEventListener.remove();
        mUserListEventListener = null;
    }
    // 原有集合绑定监听的代码...
}

3. 同步清理位置列表

在getUsers()的onEvent回调中,每次刷新用户列表时同步清空旧的位置缓存,避免重复添加:

if(queryDocumentSnapshots != null){
    // 原有清空用户列表的代码
    mUserList.clear();
    mUserList = new ArrayList<>();
    // 新增:清空旧位置数据
    mUserLocations.clear();

    for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
        User user = doc.toObject(User.class);
        mUserList.add(user);
        getUserLocation(user);
    }
}

4. 加载到位置后主动触发视角更新

修改getUserLocation()的回调,匹配到当前登录用户的位置时,直接更新mUserPosition,地图已初始化就直接调用setCameraView():

private void getUserLocation(User user){
    DocumentReference locationRef = mDb.collection("User Locations")
            .document(user.getUser_id() );

    locationRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()){
                UserLocation userLocation = task.getResult().toObject(UserLocation.class);
                // 增加空值校验,避免geo_point为空引发二次崩溃
                if(userLocation != null && userLocation.getGeo_point() != null){
                    mUserLocations.add(userLocation);
                    String currentUid = FirebaseAuth.getInstance().getUid();
                    // 匹配到当前登录用户时更新位置
                    if (user.getUser_id().equals(currentUid)) {
                        mUserPosition = userLocation;
                        // 地图已初始化就直接更新相机视角
                        if (mGoogleMap != null) {
                            setCameraView();
                        }
                    }
                }
            }
        }
    });
}

5. 修复地图初始化时的空值判断

不要在onMapReady()中直接调用setCameraView(),先确认用户位置已加载完成:

@Override
public void onMapReady(GoogleMap map) {
    // 原有权限校验、地图基础配置代码...
    mGoogleMap = map;
    // 仅当用户位置加载完成时才设置视角
    if (mUserPosition != null) {
        setCameraView();
    }
}

6. 兼容首次定位的冷启动场景

如果是第一次打开应用,Firestore中还没有当前用户的历史位置数据,本地定位保存成功后也要主动更新mUserPosition:
修改saveUserLocation()的成功回调:

locationRef.set(mUserLocation).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if(task.isSuccessful()){
            Log.d(TAG, "saveUserLocation: 本地位置已同步到Firestore");
            // 新增:本地定位保存后更新用户位置
            mUserPosition = mUserLocation;
            if (mGoogleMap != null) {
                setCameraView();
            }
        }
    }
});
额外优化建议
  • 可直接删除已无用的setUserPosition()方法,避免后续开发误用
  • 后续开发路线追踪功能时,可给UserLocation的时间戳字段建立Firestore索引,方便按日期快速查询历史轨迹

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.23 10:54:05