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

后端 未结 2 1898
面向向阳花
面向向阳花 2020-12-06 09:03

My Problem

      08-10 04:23:28.820 21501-21501/my.org.medicare.medicareapp E/AndroidRuntime: FATAL EXCEPTION: main                                                   


        
相关标签:
2条回答
  • 2020-12-06 09:14

    Most likely this is your problem, in your fetchData method:

    private void fetchData(DataSnapshot dataSnapshot)
    {
        spacecrafts.clear();
        //dataSnapshot.getChildren()
        for (DataSnapshot ds : dataSnapshot.getChildren()) //--> At this point, ds is an iterator of dataSnapshot; it will iterate the dataSnapshot's children. In this case, the first child's type is String, thus the first iteration of ds will have a type of String. 
        {
            System.out.println(ds.getValue());
            protest spacecraft=ds.getValue(protest.class); //--> at this point, you tried to assign a String to an Object with type "protest" by conversion. This is illegal, so it will throw an exception instead.
            spacecrafts.add(spacecraft);
        }
    }
    

    I hope this helps.

    0 讨论(0)
  • 2020-12-06 09:17

    Your fetchData function already receives DataSnapshot as a child-node, but even here you still loop through the children which will effectively be looping through individual keys (name, contact, centre). You should simply call getValue on DataSnaphot without looping:

    private void fetchData(DataSnapshot dataSnapshot)
        {
            spacecrafts.clear();        
            protest spacecraft=dataSnapshot.getValue(protest.class);
            spacecrafts.add(spacecraft);        
        }
    

    I hope this helps.

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