2

I am working on Android project, which will allow users to find nearest petrol pump's name, address, and most importantly their prices. I have a ListView, which I want to populate through Firebase list adapter because I am using Firebase for my project. Now after writing code I am getting nothing from Firebase -- it's just blank screen on Android phone (no error showing in Android Studio).

After writing setContentView(lview); in the end of the onCreate() method , it is giving me error now and the activity crashes. Finally I got the error from firebase : com.firebase.client.FirebaseException: Failed to bounce to type

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.client.Firebase;
import com.firebase.ui.FirebaseListAdapter;

public class testbezinpriser extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//  setContentView(R.layout.activity_testbezinpriser);
    ListView lview = new ListView(this);


    Firebase.setAndroidContext(this);
    Firebase ref = new Firebase("https://xxxxx.firebaseio.com");

    FirebaseListAdapter<Benzin> Adapter = new FirebaseListAdapter<Benzin>(this, Benzin.class, R.layout.listepriser, ref) {
        @Override
        protected void populateView(View v, Benzin s, int i) {
            ((TextView)v.findViewById(R.id.navnView)).setText(s.getName());
            ((TextView)v.findViewById(R.id.addressView)).setText(s.getAddress());
            ((TextView)v.findViewById(R.id.prisView)).setText(s.getPrice()));

        }
    };
    lview.setAdapter(Adapter);
    setContentView(lview); 
}
}

And Benzin class

public class Benzin {

String Address;
String Name ;
String Price;

public Benzin(){

}
public Benzin (String Name , String Address , String Price){
    this.Name = Name;
    this.Address = Address;
    this.Price = Price;
}

public String getAddress(){
    return Address;
}

public String getName(){
    return Name;
}
public String getPrice(){
    return Price;
}

}

XML Layout for listepriser

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:id="@+id/logoView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"
    android:src="@drawable/shell"
    android:contentDescription="tankstation_billed" />

<TextView
    android:layout_width="70dp"
    android:layout_height="25dp"
    android:id="@+id/navnView"
    android:layout_alignTop="@+id/logoView"
    android:layout_toEndOf="@+id/logoView"
    android:layout_marginStart="10dp"
    android:textStyle="bold|italic"
    android:textSize="15sp"
    android:textColor="#000000"
    android:text="SHELL"
    android:layout_marginTop="5dp"
    android:textIsSelectable="false" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Måløvhovedgade 38 , Måløv"
    android:id="@+id/addressView"
    android:layout_alignBottom="@+id/logoView"
    android:layout_alignStart="@+id/navnView"
    android:layout_marginBottom="3dp"
    android:textSize="12sp"
    android:textColor="#000000" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10.99"
    android:id="@+id/prisView"
    android:layout_alignTop="@+id/navnView"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="10dp"
    android:textColor="#000000"
    android:typeface="serif" />

Database view

ZZ.IT
  • 23
  • 1
  • 8
  • For a next question, add the JSON as text and not as a (link to a) screenshot. You can easily get the JSON as text by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Apr 19 '16 at 20:14

4 Answers4

1

Try changing your Firebase reference. It doesn't look like it pointing to the right location. Based on the link you posted to your data structure it looks like you are trying to list objects stored in your /detail location. If that true try using a ref like this:

Firebase ref = new Firebase("https://xxxxx.firebaseio.com/detail");
Kevin O'Neil
  • 1,411
  • 1
  • 15
  • 15
1

If you look at the full stack trace of the Failed to bounce to type exception, it will tell you what is wrong.

But in this case it seems likely that your problem is caused by the fact that the property names in your JSON start with an uppercase letter. This is likely to work:

public class Benzin {
    public String Address;
    public String Name ;
    public String Price;
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Do u think I can sort it out according to their parents. I will use geofire to calculate nearest petrol stations. Like I don't have to get all the stuff under the /Detail. Just the ones which are nearest – ZZ.IT Apr 19 '16 at 20:42
0

I haven't used Firebase before, but shouldn't the ListView be added to the layout (or be retrieved from it)?

Something like

setContentView(R.layout.activity_testbezinpriser);
ListView lview = (ListView)findViewById(R.id.station_list);

In the activity_testbezinpriser.xml, there should be a ListView, like

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ListView
        android:id="@+id/station_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>
Cliabhach
  • 1,402
  • 2
  • 12
  • 19
  • I am not sure but after putting setContentView(lview); in the end of the onCreate() method , it is giving me error now and the activity crashes. Finally I got the error from firebase : com.firebase.client.FirebaseException: Failed to bounce to type – ZZ.IT Apr 19 '16 at 19:10
  • No, hold on - the setContentView() method call in your code was correct. You have a layout file called `activity_testbezinpriser.xml`, which should have a `ListView` in it. Can you add the xml file to your question? – Cliabhach Apr 19 '16 at 19:58
  • I've added some xml. Does your layout look like this? – Cliabhach Apr 19 '16 at 20:04
  • I have uploaded xml "listepriser" – ZZ.IT Apr 19 '16 at 20:15
  • @ZZ.IT that looks ok. Can I see activity_testbezinpriser? – Cliabhach Apr 19 '16 at 20:17
  • 1
    I just had to add public in properties. Its working now. – ZZ.IT Apr 19 '16 at 20:38
0

Just a guess here. Your getPrice() method returns a string. I think you need a getter that has the same return type as the property you are getting. You can keep the String one, but it must be in addition to a method with the signature...

public Double getPrice();

The reason I think this is because firebase is complaining that it is unable to figure what property of your object a certain field matches to. This could be because you are supposed to provide getters for each property, and Firebase may not like that price is a Double but your getter turns it into a string. Here is a link to a detailed post about how this all works by Frank. Why do I get "Failed to bounce to type" when I turn JSON from Firebase into Java objects?

Community
  • 1
  • 1