DatabaseException: Can't convert object of type java.lang.String to type

前端 未结 7 569
北海茫月
北海茫月 2020-12-03 18:50

I\'ve looked at a few other answers for similar problems but don\'t understand why mine isn\'t working.

I\'m trying to get my app to read commands from Firebase and

相关标签:
7条回答
  • 2020-12-03 19:09

    In my case the problem was, that I using Kotlin class, and there were no default constructor such as

     constructor() : this(0, "")
    
    0 讨论(0)
  • 2020-12-03 19:11

    you can use constructor oop normally like this

    it's the same and no Error anymore

    CommandObject command = new CommandObject(ds.child("").getValue()....
    

    fill it and make sure your reference in firebase data should not be empty

    0 讨论(0)
  • 2020-12-03 19:13

    Alex posted the right solution for me, but since I was using firebase-ui and kotlin, the code used is different. More info here

        val options = FirebaseRecyclerOptions.Builder<ChatMessage>()
                .setQuery(query, ChatMessage::class.java)
                .build()
    
        // equivalent options object with manual parsing, use it to debug which field gives error
        val options = FirebaseRecyclerOptions.Builder<ChatMessage>()
                .setQuery(query, object : SnapshotParser<ChatMessage> {
    
                    override fun parseSnapshot(snapshot: DataSnapshot): ChatMessage {
    
                        val senderId = snapshot.child("senderId").getValue(String::class.java)
                        val receiverId = snapshot.child("receiverId").getValue(String::class.java)
                        val senderName = snapshot.child("senderName").getValue(String::class.java)
                        val text = snapshot.child("text").getValue(String::class.java)
                        val timestamp = snapshot.child("timestamp").getValue(Long::class.java)
    
                        return ChatMessage(senderId, receiverId, senderName, text, timestamp)
                    }
    
                })
                .build()
    
    
        adapterMessages = object : FirebaseRecyclerAdapter<ChatMessage, ChatHolder>(options) {
    
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatHolder {
    
                val rootView = LayoutInflater.from(parent.context).inflate(R.layout.chat_message_item, parent, false)
    
                return ChatHolder(rootView)
            }
    
            override fun onBindViewHolder(holder: ChatHolder, position: Int, model: ChatMessage) {
    
                holder.populateItem(model)
            }
        }
    
    0 讨论(0)
  • 2020-12-03 19:14

    You are getting this error:

    Can't convert object of type java.lang.String to type com.parrot.sdksample.activity.CommandObject
    

    Because you are trying to read the data of type String which is of type CommandObject and that why you are getting this error.

    A more simple way to get those values would be to use the String class like this:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference commandsRef = rootRef.child("drones").child("commands");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                boolean executed = ds.child("executed").getValue(Boolean.class);
                String text = ds.child("text").getValue(String.class);
                double timestamp = ds.child("timestamp").getValue(Double.class);
                Log.d("TAG", executed + " / " + text + " / " + timestamp);
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    commandsRef.addListenerForSingleValueEvent(eventListener);
    

    And this the approach using an object of CommandObject class:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference commandsRef = rootRef.child("drones").child("commands");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                CommandObject commandObject = ds.getValue(CommandObject.class);
                Log.d("TAG", commandObject.getExecuted() + " / " + 
                    commandObject.getText() + " / " + 
                    commandObject.getTimestamp());
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    commandsRef.addListenerForSingleValueEvent(eventListener);
    

    In both cases your output will be:

    false / TAKE_OFF / 1.512686825309134E9
    false / LAND / 1.512687248764272E9
    
    0 讨论(0)
  • 2020-12-03 19:17

    if u are using map object in your database should use get method directly with your object

    doc: DocumentSnapshot val data = doc.get("location", SaleLocations::class.java)

    0 讨论(0)
  • 2020-12-03 19:20

    I faced a similar issue where I could not retrieve the data from the database; having a DatabaseException error with a message

    can't convert from this type to that type.

    The mistake I was making was I was writing incorrectly to the database. I wrote something like ...

    mdatabaseReference.setValue(myObject)
    

    instead of

    mdatabaseReference.child(myObject.getId()).setValue(myObject)
    

    So essentially the answer I am suggesting is: you should ensure your data is written correctly to the database at the right node in the JSON tree if not you would likely get this kind of error when you are reading from the database.

    0 讨论(0)
提交回复
热议问题