0

I tried making a RecyclerView that is getting data from Firebase Firestore and it's crashing.

It's not error with Adapter because when I add data manually to ArrayList it works just fine, I commented those lines.

I excluded Importing tags from code, everything is good there. I also double-checked name of collection on Firestore and in my code and it matches.

Here is my code:

public class MainActivity extends AppCompatActivity {

    RecyclerView items;
    ArrayList<Item> itemsList;
    MyAdapter adapter;
    FirebaseFirestore db;
    ProgressDialog pD;

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

        pD = new ProgressDialog(this);
        pD.setCancelable(false);
        pD.setMessage("Retrieving Data...");
        pD.show();

        items = findViewById(R.id.recycle);
        items.setHasFixedSize(true);
        items.setLayoutManager(new LinearLayoutManager(this));

        db = FirebaseFirestore.getInstance();
        itemsList = new ArrayList<Item>();
        adapter = new MyAdapter(MainActivity.this, itemsList);

        items.setAdapter(adapter);

        itemsList.add(new Item("Test", "Test"));  //This is test line to see if error is in Adapter.

        EventChangeListener();
    }

    private void EventChangeListener() {

        db.collection("Foods")
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {

                        if (error != null){

                            Toast.makeText(MainActivity.this, "Database Error " + error.getMessage(), Toast.LENGTH_SHORT).show();
                            return;
                        }

                        for (DocumentChange dc : value.getDocumentChanges()){

                            if(dc.getType() == DocumentChange.Type.ADDED){

                                itemsList.add(dc.getDocument().toObject(Item.class)); //When I comment this line it works fine
                                //and if I add items to list manually like shown above it works fine

                            }

                            adapter.notifyDataSetChanged();

                        }
                    }
                });
    }
}

I don't get any error it just crashes.

Edit 1:

I looked into another posts connected with Logcat and every seems to just put Exception:

2021-10-28 10:50:30.801 3159-3159/com.example.recyclerview E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recyclerview, PID: 3159
    java.lang.RuntimeException: Found two getters or fields with conflicting case sensitivity for property: name
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.addProperty(CustomClassMapper.java:736)
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.<init>(CustomClassMapper.java:640)
        at com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(CustomClassMapper.java:377)
        at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:540)
        at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:253)
        at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:100)
        at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:183)
        at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:116)
        at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:161)
        at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:97)
        at com.example.recyclerview.MainActivity$1.onEvent(MainActivity.java:75)
        at com.example.recyclerview.MainActivity$1.onEvent(MainActivity.java:61)
        at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2$com-google-firebase-firestore-Query(Query.java:1133)
        at com.google.firebase.firestore.Query$$ExternalSyntheticLambda2.onEvent(Unknown Source:6)
        at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$com-google-firebase-firestore-core-AsyncEventListener(AsyncEventListener.java:42)
        at com.google.firebase.firestore.core.AsyncEventListener$$ExternalSyntheticLambda0.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8595)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Also here is a screenshot of Firebase Firestore:

enter image description here

Data is on my first language not English and also a placeholders.

61th line is .addSnapshotListener(new EventListener<QuerySnapshot>() {

75th line is itemsList.add(dc.getDocument().toObject(Item.class));

  • If an app crashes there is a stack trace written to its logcat output. Please find this and add the complete error message and stack trace to your question. Also see https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Frank van Puffelen Oct 28 '21 at 01:45
  • Please edit your question and add the information Frank van Puffelen asked for, and please also add your database structure as a screenshot. – Alex Mamo Oct 28 '21 at 07:03
  • Also, Puf, I'm sorry for late responding there is a 9hr difference between us, and I posted this question right before going to bed at 2am. – Velibor Simonovic Oct 28 '21 at 09:49

1 Answers1

0

I found a solution.

Firstly I haven't made a empty constructor in Item class.

package com.example.recyclerview;

import android.widget.ImageView;

public class Item {

    protected String Name;
    protected String Type;
    private int photo;

    public Item(String name, String type) {
        Name = name;
        Type = type;
        this.photo = R.drawable.ic_launcher_background;
    }

    
    //I added this
    public Item(){

    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public int getPhoto() {
        return photo;
    }

    public void setPhoto(int photo) {
        this.photo = photo;
    }
}

Line is commented. I found this answer on this Stack post.

After that I got empty Cardview layouts because I didn't match Firestore names with names in Item class.

Hope I help someone as well.