无法将类型 java.lang.Double 的对象转换为类型 com.newapp.elephantapplication.Data_Model

Can't convert object of type java.lang.Double to type com.newapp.elephantapplication.Data_Model

我正在尝试将我的应用程序连接到 Firebase 数据库,但每当我启动代码时都会出现问题

那是我的“LiveLocationActivity”activity

public class LiveLocationActivity extends AppCompatActivity implements OnMapReadyCallback {    
    RecyclerView recyclerView ;
    DatabaseReference db;
    Data_Adapter data_adapter ;
    ArrayList<Data_Model> list ;

    private GoogleMap mMap;
    private ActivityMapsBinding binding;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_location_acivity);

        recyclerView = findViewById(R.id.eleList);
        db = FirebaseDatabase.getInstance().getReference("GPS");
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_frag);
        mapFragment.getMapAsync(this);    

        list = new ArrayList<>();
        data_adapter  = new  Data_Adapter(this,list);
        recyclerView.setAdapter(data_adapter);
        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot : snapshot.getChildren())
                {
                    Data_Model data_model = dataSnapshot.getValue(Data_Model.class);
                    list.add(data_model);
                }
                data_adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {    
            }
        });    
    }

    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        mMap = googleMap;

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("GPS");

        ValueEventListener listener = databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                Double f_latitude = snapshot.child("f_latitude").getValue(Double.class);
                Double f_longitude = snapshot.child("f_longitude").getValue(Double.class);

//                Double f_latitude = 28.6139391;
//                Double f_longitude = 77.2068325 ;

                LatLng location = new LatLng(f_latitude,f_longitude);

                mMap.addMarker(new MarkerOptions().position(location).title("ELE00001"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {    
            }
        });
    }
}

那是我的数据模型class

public class Data_Model {
    String IdElephant,Daily_Log_Date;
    long f_latitude,f_longitude;

    public Data_Model(String idElephant, String daily_Log_Date, long f_latitude, long f_longitude) {
        IdElephant = idElephant;
        Daily_Log_Date = daily_Log_Date;
        this.f_latitude = f_latitude;
        this.f_longitude = f_longitude;
    }

    public String getIdElephant() {
        return IdElephant;
    }

    public String getDaily_Log_Date() {
        return Daily_Log_Date;
    }

    public long getF_latitude() {
        return f_latitude;
    }

    public long getF_longitude() {
        return f_longitude;
    }

    public void setIdElephant(String idElephant) {
        IdElephant = idElephant;
    }

    public void setDaily_Log_Date(String daily_Log_Date) {
        Daily_Log_Date = daily_Log_Date;
    }

    public void setF_latitude(long f_latitude) {
        this.f_latitude = f_latitude;
    }

    public void setF_longitude(long f_longitude) {
        this.f_longitude = f_longitude;
    }

    public  Data_Model (){
    }
}

和我的日志错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newapp.elephantapplication, PID: 31757
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Double to type com.newapp.elephantapplication.Data_Model
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
    at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
    at com.newapp.elephantapplication.LiveLocationActivity.onDataChange(LiveLocationActivity.java:58)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
    at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
    at com.google.firebase.database.core.view.EventRaiser.run(EventRaiser.java:55)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是我第一次使用 Firebase;我看过关于这个主题的其他问题,但我无法弄清楚如何让它发挥作用;我希望你能帮助我。

据我所知,您的 GPS 节点有三个子节点。一个是 GPSData 节点下有两个属性,然后是两个节点(f_latitudef_longitude)有数值。

由于您正在读取 GPS,然后使用 for(DataSnapshot dataSnapshot : snapshot.getChildren()) 遍历其子节点,因此 dataSnapshot 将指向这三个子节点中的每一个(GPSDataf_latitudef_longitude) 依次。虽然 GPSData 下的值与您的 Data_Model class 匹配,但其他两个节点是简单的数值,因此无法转换为 Data_Model 对象。这就是错误消息告诉您的内容。

要使当前代码正常工作,请将 f_latitudef_longitude 移动到 GPSData 节点下。