简体   繁体   中英

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.Map

I am getting the error in this line: "Map map = (Map) dataSnapshot.getValue();"

     private void getAssignedCustomer(){
    String driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference assignedCustomerRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(driverId);
    assignedCustomerRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){

                Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();

                if(map.get("customerRideId") != null){
                    customerId = map.get("customerRideId").toString();
                    getAssignedCustomerPickupLocation();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

How to get rid of this error???

Quoting from the Firebase documentation:

getValue() returns the data contained in this snapshot as native types. The possible types returned are:

Boolean

String

Long

Double

Map

List

This list is recursive; the possible types for Object in the above list is given by the same list. These types correspond to the types available in JSON.

The value returned from dataSnapshot.getValue() seems to be Boolean , and it cannot certainly be assigned to a Map<String, Object> .

Make sure that you are using the correct data from dataSnapshot

Please check this code

if (dataSnapshot.getValue() != null) {
  String avataStr = (String) dataSnapshot.getValue();
}

首先,在您的firabase数据库中检查您要在节点“用户/驱动程序”下尝试检索的关键driverId的值不是布尔值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM