0

I am using Firebase, and I am trying to show data from Firebase through my Android app. Yet, when I run the app, it crashes and logcat says "failed to bounce to type". I mimicked the properties of the JSON structure in a java class.

Firebase data

Here is the MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Firebase.setAndroidContext(this);
    Firebase ref = new Firebase("https://fbandg.firebaseio.com/");
    android.R.layout.simple_list_item_1, android.R.id.text1);
    final TextView textbox = (TextView) findViewById(R.id.textbox);
    Firebase.setAndroidContext(this);
    ValueEventListener newCon = ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            fObject obj = dataSnapshot.getValue(fObject.class); //Line 49
            textbox.setText(obj.toString());
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

Here is the Java class I created:

public class fObject
{
    String newCond;
    public String getCondition()
    {
        return newCond;
    }
}

The errors produced are the following:

firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
com.example.elish_000.myfirstapp.MainActivity$1.onDataChange(MainActivity.java:49)
elishaolade
  • 75
  • 12

2 Answers2

1

Firebase's JSON-to-Java mapper uses a JavaBean pattern to determine how to map fields. For a class to be a valid JavaBean, the field name and the getter/setter need to match.

To make your class work, change it to:

public class fObject
{
    String newCond;
    public String getNewCond()
    {
        return newCond;
    }
}

You can then read it from a DataSnapshot with:

fObject obj = dataSnapshot.getValue(fObject.class);

I've covered this extensively a while ago: Why do I get "Failed to bounce to type" when I turn JSON from Firebase into Java objects?. You should probably read that too.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

Try this,

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
Firebase ref = new Firebase("https://fbandg.firebaseio.com");
final TextView textbox = (TextView) findViewById(R.id.textbox);
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        fObject fobject = new fObject();
        fobject.setNewCondition(dataSnapshot.child("condition").getValue().toString());
        textbox.setText(fobject.getNewCondition());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

modify your fObject class,

public class fObject
{
    String newCond;
    public String getNewCond()
    {
        return newCond;
    }

    public void setNewCond(String cond)
    {
        newCond = cond;
    }
}
Parag Kadam
  • 3,620
  • 5
  • 25
  • 51