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

Firestore获取对象内数据问题:无法获取Form1与Form2数据

解决Firebase Firestore获取Form1/Form2数据时的ClassCastException问题

看你的代码和报错信息,问题很明确:你在遍历mobileMenus这个Map的时候,把里面的Boolean类型值(比如Attendance、Calender这些字段)强行转成了Map,这就触发了java.lang.ClassCastException。

问题根源

你的mobileMenus结构应该是混合类型的:一部分键(比如Attendance)对应的值是Boolean,而Form1、Form2这些键对应的值才是包含name和id的Map。但你现在遍历整个mobileMenus的所有entry,不管值的类型就直接强转成Map,遇到Boolean类型的自然就报错了。

另外,你前面把mobileMenus转成字符串再解析成JSON的操作其实完全没必要,Firestore的DocumentSnapshot已经能直接帮你处理嵌套结构,不用自己手动转JSON。

修正后的代码

直接从mobileMenus里针对性地获取Form1、Form2的数据,或者遍历的时候先判断值的类型:

registration = query.whereEqualTo("UID", sharedPref.getString("userId", null))
    .addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (e != null) {
                Log.e("FirestoreError", "Listen failed", e);
                return;
            }

            for (DocumentChange documentChange : documentSnapshots.getDocumentChanges()) {
                Map<String, Object> mobileMenus = (Map<String, Object>) documentChange.getDocument().getData().get("mobileMenus");
                if (mobileMenus == null) {
                    continue;
                }

                // 处理Boolean类型的菜单开关
                shared_menueditor.putString("isAttendance", String.valueOf(mobileMenus.get("Attendance")));
                shared_menueditor.putString("Calender", String.valueOf(mobileMenus.get("Calender")));
                shared_menueditor.putString("Expenses", String.valueOf(mobileMenus.get("Expenses")));
                shared_menueditor.putString("Leaves", String.valueOf(mobileMenus.get("Leaves")));
                shared_menueditor.putString("LogOut", String.valueOf(mobileMenus.get("LogOut")));
                shared_menueditor.putString("NoticeBoard", String.valueOf(mobileMenus.get("NoticeBoard")));
                shared_menueditor.putString("Tasks", String.valueOf(mobileMenus.get("Tasks")));
                shared_menueditor.putString("TrackEmployee", String.valueOf(mobileMenus.get("TrackEmployee")));

                // 处理Form1、Form2这类嵌套Map的字段
                // 方法1:直接根据键获取,更高效
                Object form1Obj = mobileMenus.get("Form1");
                if (form1Obj instanceof Map) {
                    Map<String, Object> form1 = (Map<String, Object>) form1Obj;
                    String form1Name = (String) form1.get("name");
                    shared_menueditor.putString("nav_form1", form1Name);
                }

                Object form2Obj = mobileMenus.get("Form2");
                if (form2Obj instanceof Map) {
                    Map<String, Object> form2 = (Map<String, Object>) form2Obj;
                    String form2Name = (String) form2.get("name");
                    shared_menueditor.putString("nav_form2", form2Name);
                }

                // 如果还有Form3等,可以同理处理
                Object form3Obj = mobileMenus.get("Form3");
                if (form3Obj instanceof Map) {
                    Map<String, Object> form3 = (Map<String, Object>) form3Obj;
                    String form3Name = (String) form3.get("name");
                    shared_menueditor.putString("nav_form3", form3Name);
                }

                shared_menueditor.apply();
                userprofile();
            }
        }
    });

关键改进点

  1. 去掉了多余的JSON转换操作,直接用Firestore返回的Map结构处理数据,更简洁也避免了手动转字符串可能出现的格式错误。
  2. 处理Boolean字段时,直接用String.valueOf()把布尔值转成字符串,不用绕JSON。
  3. 获取Form1/Form2时,先判断值的类型是不是Map,再进行强转,彻底避免ClassCastException。
  4. 直接通过键名获取Form1/Form2,比遍历整个Map更高效,也不会触发非Map类型值的转换错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:03:54