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

后端 未结 2 1917
面向向阳花
面向向阳花 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.

提交回复
热议问题