Retrive Child values Failed-Firebase- Android

sharib ahmed :

I am trying to add the cost of each list using Datasnapshot and then sending it to firebase and again using the sent data into another activity but my app opens then crashes throwing below error.

    2020-03-12 14:43:13.887 17284-17284/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.zfr, PID: 17284
    com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@19.2.1:425)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:216)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.1:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.1:203)
        at com.example.android.zfr.NavActivity$2.onDataChange(NavActivity.java:71)
        at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.2.1:179)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

And this is the activity code in which I'm using the data

nDb=database.getReference().child("Navigation Activity").child("Cost");
    nDb.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String cost=dataSnapshot.getValue(String.class);
            Log.d(TAG, "Value is: " + cost);
            nav_total_cost.setText(cost);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

Please Don't judge me for my mistakes.

Milan Pansuriya :

Change your Code this way

nDb=database.getReference().child("Navigation Activity").child("Cost");
nDb.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Long cost=dataSnapshot.getValue(Long.class);
        Log.d(TAG, "Value is: " + cost);
        nav_total_cost.setText(cost.toString());
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

You are casting wrong value your result is Long and you are casting in to String

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216497&siteId=1