0

I want to retrieve data from realtime firebase database into my android studio app. I am using TextView to display the data however it gives me an error. The data from firebase won't display in the TextView. can someone help me with this error..tqq

this is my error message

2021-06-09 03:01:09.992 11837-11837/com.fyp.airpollutiondetection E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.fyp.airpollutiondetection, PID: 11837
    com.firebase.client.FirebaseException: Failed to bounce to type
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
        at com.fyp.airpollutiondetection.reading$1.onDataChange(reading.java:30)
        at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
        at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
        at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
     at [Source: java.io.StringReader@2f78e27; line: 1, column: 1]
        at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
        at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)
        at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
            ... 11 more

I also get this message

2021-06-09 03:00:57.215 11837-11837/? E/lutiondetectio: Unknown bits set in runtime_flags: 0x8000

This is my code

package com.fyp.airpollutiondetection;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class reading extends AppCompatActivity {
    private TextView nilai;
    private Firebase mRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reading);

        nilai = (TextView)findViewById(R.id.nilai);
        mRef = new Firebase("https://air-pollution-7917c-default-rtdb.firebaseio.com/CARBON");
        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String CARBON = dataSnapshot.getValue(String.class);
                nilai.setText(CARBON);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });
    }
}
  • Have you tried to use the [latest version](https://firebase.google.com/support/release-notes/android) of the Firebase Realtime Database? – Alex Mamo Jun 09 '21 at 07:59
  • yes I have use the latest version.. It is said that the error is at this line ``` String CARBON = dataSnapshot.getValue(String.class);``` – NURUL AIDA Jun 09 '21 at 11:19
  • This is not how nowadays you instantiate Firebase, by calling the constructor. Have you checked at least the documentation? – Alex Mamo Jun 09 '21 at 11:28
  • I have check the link you give. My android studio version is 4.1.1 and I have use the latest version of firebase realtime database. This is my first time with android studio so i am not sure about what to do. Can you help me solve this problem? Should I install the latest version of android studio? – NURUL AIDA Jun 09 '21 at 11:40
  • No, the latest dependencies of Firebase, as shown in that link. – Alex Mamo Jun 09 '21 at 11:49
  • I have use the latest version of the dependencies.. It still give me the same error and the app keeps stopping. – NURUL AIDA Jun 09 '21 at 12:02

0 Answers0